Forum Moderators: phranque
Here is what I thought was right although the last change I made caused a 500 error, go me!
Options +FollowSymLinks
RewriteEngine on
RewriteRule external-page-(.*)\.htm$ external.php?page=$1 RewriteCond %{THE_REQUEST} ^([a-z+]+[a-z+])\.htm /external\.php\?page=[a-z+]\ HTTP/
RewriteRule ^external\.php$ http://www.example.com/external-page-%1.htm? [R=301,L] [edited by: jdMorgan at 3:53 pm (utc) on April 30, 2009]
[edit reason] example.com [/edit]
These are the dynamic and static, the "htm" was his request but can easily be left as php or turned into /contact_us/, whichever will work best.
Thanks (for helping again) jdMorgan
[edited by: jdMorgan at 3:54 pm (utc) on April 30, 2009]
[edit reason] example.com [/edit]
Options +FollowSymLinks
RewriteEngine on
#
# Externally redirect direct client requests for dynamic URL to corresponding static URL
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /external\.php\?page=([^&\ ]+)\ HTTP/
RewriteRule ^external\.php$ http://www.example.com/external-page-%1.htm? [R=301,L]
#
# Internally rewrite static URL requests to dynamic script filepath
RewriteRule ^external-page-(.+)\.htm$ external.php?page=$1 [L]
GET /external.php?page=contact_us HTTP/1.1
Note that if any other name/value pairs precede or follow the "page=contact_us" name/value pair in the query string attached to the requested /external.php URL-path, the rule will not be invoked. If additional parameters are a possibility, then the code must be modified to allow for them and to handle them correctly.
Also, the rule will not be invoked if the "page=" value is blank.
Jim
[edited by: jdMorgan at 3:55 pm (utc) on April 30, 2009]
Regarding the url not being there, is there a way to get around this (not that I can see it happening but I like to cover bases)
Just for my own knowledge more than anything if the pages did have secondary paremeters how would you mod the code, write and additional section or change the current one to something like this;
# Externally redirect direct client requests for dynamic URL to corresponding static URL
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /external\.php\?page=([^&\ ]+)/?id=([^&\ ]+)\ HTTP/
RewriteRule ^external\.php$ http://www.example.com/external-page-%1-%2.htm? [R=301,L]
RewriteRule ^external-page-(.+)-(.+)\.htm$ external.php?page=$1?id=$2 [L]
There is no "&" in the pattern, actually. What is there is two instances of "NOT an ampersand". It's mostly an efficiency thing, but also prevents mis-operation. The pattern "page=([^&\ ]+)\ HTTP/" says, "Match the word "page" followed by an equal sign, then match and capture one or more characters not equal to either an ampersand or a space, stop capturing when you find either one, then match a space followed by "HTTP", a slash, and then anything else or nothing (mod_rewrite won't care what follows that slash because there is no end-anchor on the pattern).
The purpose is to make the compare and capture of the "page=" value as efficient as possible using POSIX regular expressions.
You will see a lot of the ambiguous and greedy ".*" and ".+" patterns in regular-expressions examples on the Web. You will also see that I almost never use them, because when possible, a much-more specific pattern should be used for the sake of efficiency. There are many cases where large numbers of rules with multiple ambiguous subpatterns can contribute significantly to the requirement for an early server upgrade -- Bad pattern coding can seriously-affect server performance. So use the most-specific patterns you can.
In case that's obscure, I'm saying do not use "(.+)-(.+)\.htm$", use "([^-]+)-([^.]+)\.htm" or even something like "(([^-]+)(-[^-]+)*)-(([^.]+)(\.[[^.]+])*)\.htm" if needed to allow for multiple hyphens and periods in the requested URL.
Jim