Forum Moderators: phranque
#I. Resource Whitelist
#I just want it to "do nothing" and stop processing rules [L]
#if the file extension is matched (css|js|gif|ico|img|jpg|php|png)
#in given directories below the root. This would be the first rule to save time.
RewriteCond %{REQUEST_FILENAME} !^(.*)/(.+)\.(css|js|gif|ico|img|jpg|php|png)$
RewriteRule ^(d1/(.*)|d2/(.*)|d3/(.*)|d4/(.*)) - [L]
#II A. External redirects for each sub-category
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /category(.+)\.php\ HTTP/
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^category-sub-category\.php(.*) http://example.com/category/sub-category/$1 [R=301,L]
#II B. External redirect for non-canonical hostname requests to canonical hostname
RewriteCond %{HTTP_HOST} ^www\.example\.com(.*)$
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^(.*)$ http://example.com(/)?(%1)? [R=301,L]
#III A. Internal rewrite for category/sub-category
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^category/([^/]+)$ category-$1.php [L]
#III B 1. Internal rewrite for category/sub-category?querystring
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^category/([^_/]+)/([^/]+)$ category-$1.php?$2 [NC,L]
#III B 2. Internal rewrite for example.com/paramater1/parameter2
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(?!category1|category2|category3)([^/]+)/([^/]+)$ page.php?parameter1=$2&parameter2=$3 [NC,L] AddOutputFilterByType SUBSTITUTE text/html
#Substitution to example.com/paramater1/parameter2
Substitute "s|<a\ href="(.*)page\.php\?parameter1=([^=/&]+)&(?:amp;)?parameter2=([^=/&]+)">|<a href="$1/$2/$3">|i"
#Substitution to example.com/category/sub-category?querystring
Substitute "s|<a\ href="(.*)category-([^/]+)\.php\?([^/]+)">|<a href="$1/$2?$3">|i"
#Substitution to example.com/category/sub-category
Substitute "s|<a\ href="(.*)category-([^/]+)\.php">|<a href="$1/category/$2">|"
#Substitution to example.com/category
Substitute "s|<a\ href="(.*)category\.php">|<a href="$1/category">|" RewriteCond %{REQUEST_FILENAME} !^(.*)/(.+)\.(css|js|gif|ico|img|jpg|php|png)$
RewriteRule ^(d1/(.*)|d2/(.*)|d3/(.*)|d4/(.*)) - [L]
I assume the RewriteCond is intended to intercept requests containing exactly one directory slash. But as written it will instead match all requests containg one or more directory slashes. The pattern you want is RewriteRule ^category-sub-category\.php(.*) http://example.com/category/sub-category/$1 [R=301,L]
I don't think this rule does what you intend it to do, unless you've got weird URLs. The body of the rule looks only at the path. If you're trying to capture a query string, that needs to go in a RewriteCond. Two of 'em in fact, to avoid infinite loops: RewriteCond %{THE_REQUEST} \?
RewriteCond %{QUERY_STRING} (.*)
RewriteRule ^category-sub-category\.php http://example.com/category/sub-category/%1 [R=301,L]
Note the change from $ to % to match a capture from a condition. You can only capture from the most recently matched condition (generally that means the one right before the Rule itself) so pay attention to order of Conditions. II B. External redirect for non-canonical hostname requests to canonical hostname
RewriteCond %{HTTP_HOST} ^www\.example\.com(.*)$
This should be the very last external redirect, and the condition is best expressed as Does mod_rewrite handle + signs in urls by default
Should I be escaping the spaces, ., /, and ? in the substitution regexes?
What is the significance of using quotation marks around the entire substitution pattern|replacement?
I assume the RewriteCond is intended to intercept requests containing exactly one directory slash. But as written it will instead match all requests containg one or more directory slashes.
#I. Resource Whitelist
RewriteCond %{REQUEST_FILENAME} ^(css/(.*)|fonts/(.*)|images/(.*)|js/(.*))
RewriteRule ^(.*)/(.+)\.(css|js|gif|ico|img|jpg|php|png) - [L]
#II A. External redirects for each sub-category
RewriteCond %{THE_REQUEST} \?
RewriteCond %{QUERY_STRING} (.*)
RewriteRule ^category-sub-category\.php http://example.com/category/sub-category/%1 [R=301,L]
You can only capture from the most recently matched condition (generally that means the one right before the Rule itself) so pay attention to order of Conditions.
#II B. External redirect for non-canonical hostname requests to canonical hostname
RewriteCond %{HTTP_HOST} !^(example\.com)?$
RewriteRule ^(.*)$ http://example.com [R=301,L]
There is absolutely no point to doing a filename lookup here; in fact you've got way too many of them everywhere.
#III A. Internal rewrite for category/sub-category
RewriteRule ^category/([^/]+)$ category-$1.php [L]
#III B 1. Internal rewrite for category/sub-category/querystring
RewriteRule ^category/([^_/]+)/([^/]+)$ category-$1.php?$2 [L]
#III B 2. Internal rewrite for example.com/paramater1/parameter2
RewriteRule ^(?!category1|category2|category3)([^/]+)/([^/]+)$ page.php?parameter1=$2&parameter2=$3 [L]
#Substitution to example.com/paramater1/parameter2
Substitute s|<a\ href="(.*)page\.php\?parameter1=([^=/&]+)&(?:amp;)?parameter2=([^=/&]+)">|<a href="$1/$2/$3">|i
#Substitution to example.com/category/sub-category?querystring
Substitute s|<a\ href="(.*)category-([^/]+)\.php\?([^/]+)">|<a href="$1/$2?$3">|i
#Substitution to example.com/category/sub-category
Substitute s|<a\ href="(.*)category-([^/]+)\.php">|<a href="$1/category/$2">|
#Substitution to example.com/category
Substitute s|<a\ href="(.*)category\.php">|<a href="$1/category">|
I'm assuming multiple captures in a condition may be accessed with %1... %N.
RewriteRule \.(css|js|gif|ico|img|jpg|php|png)$ - [L] RewriteRule ^(css|fonts|images|js)/ - [L] Now, about that "stop right here" rule. Do there exist supporting files that you do want to redirect? If not, you could always just say...I'll have to take a look and double check once the test environment is set up and I can actually make sure that these urls are matching up with the current ones. There are one or more directories that do need redirection and that was the reasoning behind explicitly listing those that did not.
But, on the other hand, your css etc directories don't contain any pages, do they? If they don't, it's even easier-- for both you and the server-- to say...The majority of the directories do not contain pages. I can always start out with the general rule referencing the directories and see how it functions. If need be then I can get more specific.
#Substitution to example.com/category
Substitute s|<a\ href="(.*)category\.php">|<a href="$1/category">| With my limited knowledge, all I can say is that the server did not like the escaped space. When I tried beginning with the href=, #Substitution to example.com/category
Substitute s|href="(.*)(category)\.php">|href="$1$2| things worked like a charm for static links. <option value="/category.php?parameter1=value1">
<option value="/category.php?parameter1=value2">
<option value="/category.php?parameter1=value3"> so phranque will come around by and by to deal with questions specific to mod_substitute.