Redirection from the php file - php

I'm looking for a little help from someone who knows PHP,
in short, it looks like this:
I once made a website for a customer with products on the Jomla CMS system, and the product was located, for example, at:
www.mysite.pl/index.php/productall/product1
www.mysite.pl/index.php/productall/product2
e.t.c...
currently his site has been changed and is in the classic html5 / css / js without any CMS, but in the same old directory
and the product is on:
www.mysite.pl/product1.html
The client created QR codes for himself, but to the old page and printed on each product
and now I would like to create an index.php file, which will redirect the traffic depending on the call in the browser, because now as the QR code is being scanned it jumped out because there is no such adreas as before.
For now, I created an index.php file that redirects to
www.mysite.pl/allproduct.hml
where is the list of all products but I would prefer it to be as before
that is, each QRcode was redirected to the chosen product.
Can you do it somehow? PLEASE HELP
now i have this code in index.php

Instead of messing with PHP, You could try adding redirect commands within the htaccess file? (if you can still use one?) The ".htaccess" file should be located in the site root.
ie: /public_html/.htaccess
within this file you can create redirects from the old address to the new one.
Redirect 301 /oldProductLink1.html http://www.example.com/NewProductPage1.html
Redirect 301 /oldProductLink2.html http://www.example.com/NewProductPage2.html
:)

There's no easy solution you must create a .htacess file and place on your root folder then redirect all the old urls to new url for each product
Redirect 301 /index.php/productall/product1 www.mysite.pl/product1.html
Redirect 301 /index.php/productall/product1 www.mysite.pl/product2.html
Redirect 301 /index.php/productall/product1 www.mysite.pl/product3.html
Redirect 301 /index.php/productall/product1 www.mysite.pl/product4.html

If the question is only about PHP and there is no way to use web server URL rewriting or redirect, then you should create files like :
/index.php/productall/product1
/index.php/productall/product2
Where index.php would be a directory. Each file should contain something like :
header('location: /product1.html');
exit();
Where the product number should be set according to file name.

i still have this:
The resource you are looking for has been removed, had its name changed, or is temporarily unavailable.
I removed this my test index.php file and created .htaccess in the root directory with yours codes

Related

HTML URL shortcuts instead of page name

I am not exactly sure the exact words to google.
How do I rename the url link to a simpler one for my webpages?
example:
from:
websitename.com/about_us_what_we_do.php
to:
websitename.com/what-we-do
another example is to let the homepage just show the address
from:
websitename.com/main.php
to
websitename.com/
You can use url rewriting using .htaccess
In .htaccess you have to mention the url that what you want & which will redirect you to actual page.
Exp :
browser url will be: domain_name/what-we-do
& Actual url will be : domain_name/about_us_what_we_do.php
in .htacces you have to mention :
RewriteRule ^what-we-do$ /about_us_what_we_do.php [L]
Check this ref:
I have short the URL Using .htaccess but every shortened URL goes to the same page
This can be done by creating a subfolder with the name what-we-do
and renaming your file about_us_what_we_do.php to index.php.
Then, you'll have your site root directory. Inside you'll have a folder named what-we-do with an index.php file inside.
If you want to do things right, you should look into VirtualHosts ( assuming you're managing the Apache server)

redirect on home page when user write index file name in url

Bellow is my site directory structure:
project-folder/
index.php
Now I have access project using folder name like www.project-folder.com and also www.folder-name.com/index.php
Problem
If some one user enter www.folder-name.com/index.php in URL then redirect on www.project-folder.com using htaccess.
How can I do this ?
Below line is I tried in htaccess
Directory Index public/index.php
Use this on the htaccess file of www.folder-name.com
Redirect index.php http://www.project-folder.com/index.php
To not make confusion, use this kind of redirect:
Redirect /path/to/old/file/old.php http://www.redirectedwebsite.com/path/to/new/file/new.php

url extension remove with php

I have a dilemma here...
At the moment I'm launing a website and there are a lot of old url that needs to be redirected to it's new url without .stm extension. And the new site is running Durpal.
Ex.
http://www.foo.com/foo/bar.stm --> http://www.foo.com/foo/bar
So I went looking more into nginx.conf setting to rewrite the url before it even hit setting.php that is being used by drupal. Turns out that my host doesn't let me touch the conf file on the root folder. but my only solution is add this to the conf file in root
server { location / { rewrite ^(.*)\.stm$ $1 permanent; } }
At this point, I'm running out of options but to redirect every pages in Drupal manually ( that is roughly about 650 pages )
I've also tried every other modules, but non offers extension strip redirect.
My other option is using 404 landing page to run php and redirect any url with .stm and redirect with scrubbed url without .stm and stays in 404 if it's not a .stm url.
This is my best bet, URL rewriting with PHP
But it's over complicated just for a extension strip and redirect?
You should be able to check if the requested URI contains ".stm", and if so then redirect to the exact same URI with the ".stm" ripped. Put something like this in your settings.php:
if (strrpos(request_uri(), '.stm') !== FALSE) {
header("Location: " . str_replace('.stm', '', request_uri()));
exit;
}

standard method for redirect on manual input url

So i'm wondering how you professionals go about securing your website by not allowing a user to manually input a url to a certain folder such as website.com/images/ and view a directory listing. The method i've been using is placing an index.php file and using this code, which just brings the user back to main homepage. But i feel like there is probably an easier way to do this. Possibly Server Side?
<?php
ob_start();
/* This will give an error. Note the output
* above, which is before the header() call */
header('Location: ../index.php');
exit;
?>
Use htaccess to prevent direct access
create .htaccess file add this Options -Indexes line . add it in your project root path
These links may help you
1. deny directory listing with htaccess
2. http://viralpatel.net/blogs/htaccess-directory-listing-enable-disable-allow-deny-prevent-htaccess-directory-listing/

redirect to mobile website using PHP

I have use this to generate this code:
<?php
require_once('mobile_device_detect.php');
mobile_device_detect(true,false,true,true, true,true,true,'http://m.mydomain.com',false);
?>
But the only directions are to "copy and paste this code". Um.. copy and paste where? Do I need to create a new php file? Is this index.php? What if I already have an index.html file?
EDIT: I understand that I put mobile_device_detect.php in the root of mydomain.com. My question is where to put the above php code.
Copy and paste this at the beginning of your PHP based pages that you want to detect the visitors for their device. If your server parses HTML files as PHP which I doubt then add this in your HTML files as well. If you're just building the website then yes you need this in files which are parsed by the PHP engine for example: ".php".
If you paste this in page that is HTML and not parsed by the server you'll see this same code as output which will do nothing. In order to have it working you need it in PHP files.
If your script is well written and well structured you may need to include it in only one place. It all depends how your website is structured.
------ UPDATE ------
Why you shouldn't be using this class? It have a special license which is not absolutely free.
Instead you can use this simple class: https://github.com/serbanghita/Mobile-Detect
Download Mobile_Detect.php
Include the file at the top in your PHP page where you want the device to be checked:
// Include the mobile device detect class
include 'Mobile_Detect.php';
// Init the class
$detect = new Mobile_Detect();
// And here is the magic - checking if the user comes with a mobile device
if ($detect->isMobile()) {
// Detects any mobile device.
// Redirecting
header("Location: http://your_redirected_url.com"); exit;
}
Creating rewrite rules for using html extension.
If you still want to use '.html' as extension just create rewrite rule that will rewrite your .php as .html. Or otherwise said create your_page_name.php and add the PHP code there. Create .htaccess file in the same DIR and add the following lines:
RewriteEngine On
RewriteBase /
RewriteRule ^your_page_name.html/?$ your_page_name.php [L]
Save, close! Now you should be able to use your php page with .html extension. To access your page now just type: http://yourdomain.com/your_page_name.html
Simple as that!
Suggestion: If I was you I'd add the rewrite rules in the web server's config file. It will be faster and more secure. But that's another lesson. If you decide to use this method just search the Stack.
Copy and paste the code anywhere you want. Just make sure the function is defined on any page that needs it.
You should either buy the script mobile_device_detect.php from the site or use a free method called pay with a tweet option.. Go to the download page and you will see them there..
ok, in case this helps someone, here are the details of what's working for me:
Create an index.php file with just this:
<?php
require_once('mobile_device_detect.php');
$mobile = mobile_device_detect();
// redirect all mobiles to mobile site and all other browsers to desktop site
if($mobile==true){
header('Location:http://m.yourdomain.com/');
}else{
header('Location:http://yourdomain.com/index.html');
}
exit;
?>
Drop the mobile_device_detect.php file in the root of your site.
Then, add this line to your .htaccess file:
DirectoryIndex index.php index.html

Categories