homepage Welcome to WebmasterWorld Guest from
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Pubcon Platinum Sponsor 2014
Home / Forums Index / Code, Content, and Presentation / Apache Web Server
Forum Library, Charter, Moderators: Ocean10000 & incrediBILL & phranque

Apache Web Server Forum

Best way to redirect a RewriteRule :)

WebmasterWorld Senior Member 10+ Year Member

Msg#: 4015106 posted 6:12 pm on Oct 28, 2009 (gmt 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!



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

Msg#: 4015106 posted 8:21 pm on Oct 28, 2009 (gmt 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.


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

Msg#: 4015106 posted 3:03 am on Oct 29, 2009 (gmt 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.


Global Options:
 top home search open messages active posts  

Home / Forums Index / Code, Content, and Presentation / Apache Web Server
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved