Forum Moderators: phranque
I'm trying to redirect the following URL as so..
[mysite.com...]
TO
[mysite.com...]
Seems simple, but I've tried both of the following and neither works (they both redirect to the intended URL but the page cannot be found). Any help would be greatly appreciated, thanks. :)
Attempt 1:
RewriteCond %{QUERY_STRING} ^(.*&)?page=([^&]+)(&.*)?$
RewriteRule ^module\.php$ /%2? [R=301,L]
Attempt 2:
RewriteCond %{QUERY_STRING} &?page=([^&]+)
RewriteRule ^module\.php$ /%1? [R=301,L]
[edited by: eelixduppy at 2:19 am (utc) on Feb. 24, 2009]
[edit reason] disabled smileys [/edit]
Which URL do you want users to see and use? You need to put *that* URL in the links in your pages.
You'll then need a rewrite (not a redirect) to connect those URL requests to the real location of the content. To be clear; a rewrite does not "make" URLs. URLs are defined by the links on your pages. It is the rewrite that connects incoming URL requests to the process that will fetch the actual content.
You'll then also need a redirect to stop the "real" location of the content from serving the file; that redirect will redirect external requests over to the URL that you want people to actually use when they request the content. The redirect will be listed before the rewrite.
Looks like you have this last part working (the redirect) but haven't done anything about the needed rewrite. All this code will use RewriteCond and RewriteRule.
The original URL is:
[mysite.com...]
I'd like users to see:
[mysite.com...]
Once the rewrite is working properly, I'll be linking to this page via the latter URL.
The page's URL is now defined as www.example.com/Help on the Web, and clients (browsers and search engine robots) will now find and use that URL.
Now step 2. We need to make sure that the http://www.example.com/module.php?page=Help URL can no longer be used. We need to permanently redirect it to http://www.example.com/Help to get browsers using old links or bookmarks to the correct URL, to tell search engines to use the new URL instead of the old, and to attribute all PageRank and link-popularity of the old URL to the new. Furthermore, this redirect must be invoked only if /module.php?page=Help is being requested directly by the HTTP client, and not as the result of any internal rewrite, such as we're about to do in step 3. If this required conditional redirect is not implemented properly, then the result would be an 'infinite' redirect/rewrite loop.
Finally, step 3 is to tell the server what to do with a client request for http://www.example.com/Help. Because this URL does not resolve to a physically-existing file, we must use a rewrite to tell the server how and where to call your script to generate the content for the page corresponding to that URL.
The code below implements steps 2 & 3. You must handle step 1 in your script or CMS database.
# Externally redirect direct HTTP client requests for dynamic URL to static URL
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /module\.php\?([^&]+&)*page=Help&?
RewriteRule ^module\.php$ http://www.example.com/Help? [R=301,L]
#
# Internally rewrite static URL requests to the module.php script
RewriteRule ^Help$ /module.php?page=Help [L]
This code allows --but does not preserve-- additional name/value pairs receding or following "page=".
Jim