Forum Moderators: phranque

Message Too Old, No Replies

Mod Rewrite

how to work with it?

         

kkonline

7:47 am on Aug 21, 2007 (gmt 0)

10+ Year Member



Currently i have a site structure like http://www.example.com/article/news.php?cat=1&ID=53
Now i want it to look like
http://www.example.com/article/news/1/53 or similar

I have heard that this can be performed incase of Apache using modrewrite

My problem is that i have already mentioned http://www.example.com/news.php?cat=1&ID=53 in many scripts and cannot change it... or i don't want to. just want that when user goes to http://www.example.com/article/news.php?cat=1&ID=53 the url he views on browser be http://www.example.com/article/news/1/53

Is that possible? How? give me some tips on modrewrite too if any. Or where can i get easy to use tutorial for modrewrite (.htaccess file i guess has to be changed. Correct?)

venelin13

10:12 am on Aug 21, 2007 (gmt 0)

10+ Year Member



The URL user see at his browser is the link user click on. So, you must change your URLs.

A little help on mod_rewrite:

Save the code below to an .htaccess file and place it into the "article" directory.

RewriteEngine On
RewriteRule /news/$1/$2 /news.php?cat=$1&ID=$2 [L]

g1smd

10:14 am on Aug 21, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



If you want the user to see that http://www.example.com/article/news/1/53 URL in the browser then .htaccess is going to have to force a 301 REDIRECT to that URL, and then .htaccess is going to need to REWRITE that URL so that the server fetches the content from the real filesystem location.

You could leave it at that, but there is a risk of confusing the bots a little, in that none of the links on your site directly link to the actual content. Every click the user makes goes through a redirect to get to the content (the rewrite is invisible to the user).

Because of that, it is far better that your page generation scripts be modified to mention the http://www.example.com/article/news/1/53 style of URL. In that way, that is what both the user and the bot actually sees, and that is what gets very easily indexed.

This question is asked every day on the forum. Check out this thread for an example... [webmasterworld.com...]

That example redirects a single internal URL to "/" and rewrites "/" to pull content from that same internal URL. It can be easily modified to work with dynamic and static URLs. There are hundreds of similar examples here.