Question

How to convert a 301 redirect dynamic url to a static url?

In this case

www.example.com/book.php?book=title

into

www.example.com/book/title

When Requesting the Dynamic URL it should Convert as a static URL. The dynamic URL should be converted and the static URL should be displayed in the address bar instead.

I got this code :

Options +FollowSymLinks
RewriteEngine on
RewriteRule ^user/([^-]+)/?$ /user.php?id=$1 [NC,L]

Can some one give me a 301 redirect of dynamic url tio static url. Thanks in Advance.

Was it helpful?

Solution

The easiest way is using Apache's mod_rewrite in your .htaccess file. To get the 'static' (seo/fancy) url in the address bar, you'll need an external redirect. To get the server to do something sensible with it, you'll need an internal rewrite. To prevent an infinite loop, you'll need to use the %{THE_REQUEST} trick.

%External redirect
RewiteCond %{THE_REQUEST} ^(GET|HEAD)\ /book\.php\?book=(.*)\ HTTP
RewriteRule ^ /book/%1? [R,L]

%Internal rewrite
RewriteRule ^book/(.*)$ /book.php?book=$1 [L]

The above rules are untested (but they should work). If they work, change [R,L] to [R=301,L]. This will make the redirect permanent. This will cache the redirect in the browser and lessen the amounts of requests to your server and make search engines 'remember' the right url. You don't want to do this while testing though, because it might show a previous attempt (that is cached) instead of sending the request actually to the server.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top