Hello guys, Will you please help me to solve this problem. How to create a mod rewrite for this problem. i have the following url locate.php?a=numbers; //combination of numbers 0-9 Code (markup): Instead of that url, I made it search engine friendly, the new url now is location/numbers.html; //combination of numbers 0-9 Code (markup): I then create a mod re-write RewriteEngine on RewriteRule ^location/([0-9-]*).html$ locate.php?a=$1 [L,NC] Code (markup): The above mod rewrite work fine... but my problem is When someone access directly the file locate.php?a=numbers; //combination of numbers 0-9 How can I redirect someone else who open the locate.php to the search engine friendly url? example locate.php?a=12 -> location/12.html Thanks in advance
You can use if(parse_url($url, PHP_URL_QUERY)) which can be used to check whether there is query string in the url or not. If it wont work for you let me know, as its working for me fine
It will be something like this: RewriteEngine on RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_FILENAME} !-l RewriteRule ^locate.php?a=(.*)$ location/$1.html [R=301] RewriteRule ^location/([0-9-]*).html$ locate.php?a=$1 [L,NC] Code (markup):
Slight change... the trick is [R=301] for redirect RewriteEngine on RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_FILENAME} !-l RewriteRule ^locate.php?a=(.*)$ http://%{HTTP_HOST}location/$1.html [R=301] RewriteRule ^location/([0-9-]*).html$ locate.php?a=$1 [L,NC] Code (markup):
Have you tried this keeping this code on the top line of your php page? if(isset($_GET['a'])) { header("Location:location/".$_GET['a'].".html"; } PHP:
With this solution I keep getting the following browser error message seems infinite loop The page isn't redirecting properly
Reason you are getting an infinite loop here is that when you hit /locations/[0-9].html it actually renders it as locate.php?a=[0-9], so $_GET['a'] is always set, and ends up in an infinite loop to itself.