: Error 404 after rewrite query strings with htaccess I'm trying to redirect the URLs of a client's website like this: www.localsite.com/immobile.php?id_immobile=24 In something like this: www.localsite.com/immobile/24.php
I'm trying to redirect the URLs of a client's website like this:
localsite.com/immobile.php?id_immobile=24
In something like this:
localsite.com/immobile/24.php
I'm using this rule in .htaccess but it returns a 404 error page.
RewriteEngine On
RewriteCond %{QUERY_STRING} ^id_immobile=([0-9]*)$
RewriteRule ^immobile.php$ localsite.com/immobile/%1.php? [L]
I have tried many other rules, but none work. What can I do?
More posts by @Jessie594
2 Comments
Sorted by latest first Latest Oldest Best
RewriteCond %{QUERY_STRING} ^id_immobile=([0-9]*)$
RewriteRule ^immobile.php$ localsite.com/immobile/%1.php? [L]
Like you say in your first sentence, you want to redirect from one URL to the other. Currently your RewriteRule performs an internal rewrite, not an external redirect, which I believe is your goal. Simply add the R=301 flag to your current RewriteRule:
RewriteRule ^immobile.php$ localsite.com/immobile/%1.php? [R=301,L]
(Is there any reason to keep the .php extension?)
This should also come towards the top of your .htaccess file.
However, this is only half the process, which is probably why you are getting a 404. You will need to do an internal rewrite back to the actual URL (with a query string) in order to complete the process (careful to avoid a redirect/rewrite loop).
Try something like this:-
RewriteEngine On
RewriteRule id_immobile/(.*)/? immobile.php?id_immobile= [NC,L]
Let me know how that looks and what the end result is if it doesn't work...
Edit
Please could you just test something for me by trying this one (from here):-
RewriteEngine On
RewriteBase /
# To externally redirect /dir/foo.php?id=123 to /dir/foo
RewriteCond %{THE_REQUEST} ^GETs([^.]+).php?id=([^&s]+) [NC]
RewriteRule ^ %1/%2? [R,L]
# To internally forward /dir/foo/12 to /dir/foo.php?id=12
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+?)/([^/]+)/?$ .php?id= [L,QSA]
# To externally redirect /dir/foo.php to /dir/foo
RewriteCond %{THE_REQUEST} ^GETs([^.]+).phps [NC]
RewriteRule ^ %1 [R,L]
# To internally forward /dir/foo to /dir/foo.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteCond %{QUERY_STRING} ^$
RewriteRule ^(.*?)/?$ .php [L]
Terms of Use Create Support ticket Your support tickets Stock Market News! © vmapp.org2024 All Rights reserved.