Welcome to WebmasterWorld Guest from

Forum Moderators: Ocean10000 & incrediBILL & phranque

Message Too Old, No Replies

Best way to redirect a RewriteRule :)

6:12 pm on Oct 28, 2009 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Sept 28, 2001
posts: 779
votes: 0

Hi all,

I'm making a change to URL structure and wondering what would be the most efficient way to do it.

Right now I have a url like "pagename.php?var=new"

Which is being rewritten with the following rule:

RewriteRule ^directory/(.*)/$ directory/pagename.php?var=$1

And that generates a url like:

So now I'd like to change it so the urls look like:

I can make the new rewrite rule easily enough:

RewriteRule ^directory/(.*).html$ directory/pagename.php?var=$1

However what's the best way to redirect the old URLs (directory/new/) to the new URLs (directory/new.html)?

I've tried a couple rules but they didn't work out for me.

Any help is always appreciated!

8:21 pm on Oct 28, 2009 (gmt 0)

Senior Member

WebmasterWorld Senior Member g1smd is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:July 3, 2002
votes: 0

A rewrite does not 'generate' a new URL. URLs are defined by the URLs in the links on your pages.

Change those links to show the URL that you want users to see and use. Then construct a RewriteRule that accepts those URL requests and fetches an alternative file and path inside the server.

You now also need two redirects. One accepts URL requests with parameters. The other accepts URL requests for the old URLs. Both redirect to the new URL format. This tidies up any traffic coming from other sites, as well as forcing searcheingines to update their index.

3:03 am on Oct 29, 2009 (gmt 0)

Senior Member

WebmasterWorld Senior Member jdmorgan is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:Mar 31, 2002
votes: 0

A couple of points to clarify and make things easier for you (and everyone)...

You are apparently confusing URLs with filepaths, misunderstand the 'direction' of RewriteRule's action, and therefore probably also misunderstand both *when* mod_rewrite takes effect, and also what it does when it acts.

This rule:

 RewriteRule ^directory/(.*)/$ directory/pagename.php?var=$1 

Rewrites an incoming client-requested URL of "example.com/directory/<value>/" to an internal server filepath of "/pagename.php?var=<value>"

So, it is not 'generating' anything at all, it is merely 'connecting' the static-looking, SEO-friendly URL to the correct server filepath, and moving the 'subdirectory' name into the query string variable "var", where your "pagename.php" script can access it as a "GET variable."

The URLs are defined in the <a href="new/"> links in your pages' HTML code -- That is where they are 'generated' -- not by some 'magic' of mod_rewrite.

Your rule could be more-efficiently coded as either

 RewriteRule ^directory/([^/]+)/$ directory/pagename.php?var=$1 [L] 

 RewriteRule ^directory/(.+)/$ directory/pagename.php?var=$1 [L] 

depending on whether you want to accept slashes within "var".

So all that said, please post an example of your best-effort at coding the old-to-new URL redirects, as a basis for discussion.



Join The Conversation

Moderators and Top Contributors

Hot Threads This Week

Featured Threads

Free SEO Tools

Hire Expert Members