To alter the URL /mobiles.php?id=5 to /mobiles/5

This content of .htaccess file is the following:

Options +FollowSymlinks
RewriteEngine on

RewriteRule /mobiles/$1 ^/mobiles.php?id=([0-9]+)$

But nonetheless it's showing /mobiles.php?id=5 within the address bar. Help. Can there be other things must be put in the .htaccess file?


  1. mod_rewrite module is enabled
  2. I've restarted Apache server after making changes towards the .htaccess file
  3. .htaccess file is within htdocs folder of Apache.
  4. I'm using Home windows + PHP + Apache + MySQL

In case your only concern would be that the old url stays within the address bar, and also you want this not to occur, try adding an [R] in the finish.

RewriteRule ^/mobiles.php?id=([0-9]+)$ /mobiles/$1 [R]

Have you really begin to see the correct page?

Incidentally, the rewrite rules generally go another way. I'd expect to determine something similar to:

RewriteRule ^/mobiles/([0-9]+)$ /mobiles.php?id=$1 

Is the concern among ensuring a URL with query parameters doesn't display in the address bar?

This works best for me:

Options +FollowSymlinks

RewriteEngine On
RewriteBase /

RewriteCond %{REQUEST_FILENAME} !-d 
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^mobiles/([0-9]+)$ mobiles.php?id=$1&rew [L]

RewriteCond %{QUERY_STRING} ^id=([0-9]+)$
RewriteRule ^mobiles.php$ /mobiles/%1? [R,L]

If you notice this line:

RewriteRule ^mobiles/([0-9]+)$ mobiles.php?id=$1&rew [L]

I've added rew variable within the query string to avoid Apache to fall within an infinite loop

When Apache execute this line:

RewriteCond %{QUERY_STRING} ^id=([0-9]+)$

Would be to make certain that url is not rewritten for Apache

Basically understand properly, you would like

  • Internally redirect /mobiles/5 to /mobiles.php?id=5
  • Also redirect the browser TO /mobiles/5 if your user navigates to /mobiles.php?id=5

With this you'll need 2 rules someone to internally rewrite the URL for first situation and second for browser redirection.

It can be done such as this:

RewriteEngine on

# for internal rewrite
RewriteRule ^/?mobiles/([0-9]+)/?$ /mobiles.php?id=$1 [L]

# for browser redirect
RewriteRule ^/?mobiles\.php\?id=([0-9]+)$ /mobiles/$1/ [R,L]

You do the alternative, ought to be:

RewriteRule ^/something/mobiles/([0-9]+)$ /something/mobiles.php?id=$1