Forum Moderators: phranque
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:
site.com/directory/new/
So now I'd like to change it so the urls look like:
site.com/directory/new.html
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!
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.
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] 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.
Thanks,
Jim