Webmasters Stack Exchange is a question and answer site for pro webmasters. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm trying to redirect the URLs of a client's website like this:


In something like this:


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$ http://localsite.com/immobile/%1.php? [L]

I have tried many other rules, but none work. What can I do?

share|improve this question

Try something like this:-

RewriteEngine On
RewriteRule id_immobile/(.*)/? immobile.php?id_immobile=$1 [NC,L]

Let me know how that looks and what the end result is if it doesn't work...


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} ^GET\s([^.]+)\.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 ^(.+?)/([^/]+)/?$ $1.php?id=$2 [L,QSA]

# To externally redirect /dir/foo.php to /dir/foo
RewriteCond %{THE_REQUEST} ^GET\s([^.]+)\.php\s [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 ^(.*?)/?$ $1.php [L]
share|improve this answer
Thank you for the answer. I've tryed with your code but it doesn't work. The URL before mod_rewrite and after is the same: localsite.com/immobile.php?id_immobile=XXX – Cristian Feb 18 '14 at 11:21
Are you sure you implemented it correctly as it should definitely have changed the URL. What else do you have in your .htaccess? – zigojacko Feb 18 '14 at 11:45
This is my .htaccess (without the MOD_Deflate compression): RewriteEngine on RewriteBase / RewriteRule ^index.php$ terrenocasa.it [R=301,L] RewriteEngine On RewriteRule id_immobile/(.*)/? immobile.php?id_immobile=$1 [NC,L] errorDocument 404 terrenocasa.it/errore.php <Files .htaccess> order allow,deny deny from all </Files> – Cristian Feb 18 '14 at 12:04
Sorry for the format. It looks very confused. But I can't answer to my question for now with preformatted text. – Cristian Feb 18 '14 at 12:07
@Christian - please see my updated answer. – zigojacko Feb 18 '14 at 12:38
RewriteCond %{QUERY_STRING} ^id_immobile=([0-9]*)$
RewriteRule ^immobile\.php$ http://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$ http://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).

share|improve this answer
No, there is no reason to keep the php extension. I had already tried with R=301 but it didn't work. Maybe I've done things uncorrectly. Sorry for the ignorance. What do you mean with an internal rewrite to the actual URL? I have to do another RewriteRule in htaccess? – Cristian Feb 18 '14 at 15:24
"it didn't work"? - You got a 404 presumably? Because... /immobile/24.php is your "pretty" URL - it is virtual and doesn't exist as an actual file on the system. You need to internally rewrite it back to the real URL ie. /immobile.php?id_immobile=24. You have presumably already changed all your links to the /immobile/24.php format? (I assume you are implementing "pretty" URLs?) – w3dk Feb 18 '14 at 15:30
Geoff has been trying to do the second part (internal rewrite) for you. Geoff's code (top bit) should be used in addition to the redirect above (with the R=301 flag). You need both parts. – w3dk Feb 18 '14 at 15:34
Thanks to both, I do not understand anything.. – Cristian Feb 18 '14 at 15:45
Take a deep breath, have a cup of tea... ;) Well, you did specifically ask about a "redirect" - which I have answered above. However, it does look like you are implementing what is commonly called "pretty (or user friendly) URLs" on an existing site. Is this the case? If so, you need to know the difference between an "external redirect" and an "internal rewrite". Also, if this is an existing site and you are changing an existing URL structure (which is what I assumed) then you will need more than just a single rewrite. – w3dk Feb 19 '14 at 13:29

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.