| 7:10 pm on Apr 4, 2012 (gmt 0)|
in directory context the left-hand side/matching phrase has the current directory stripped from the url path including the trialing slash of the directory:
|Note: Pattern matching in per-directory context |
Never forget that Pattern is applied to a complete URL in per-server configuration files. However, in per-directory configuration files, the per-directory prefix (which always is the same for a specific directory) is automatically removed for the pattern matching and automatically added after the substitution has been done.
| 7:35 pm on Apr 4, 2012 (gmt 0)|
Ah, I missed that. Thanks!
| 7:39 pm on Apr 4, 2012 (gmt 0)|
In the RegEx pattern in the rule in the .htaccess file in the products folder, don't mention the name of the folder the file is in as explained above.
Additionally, make sure the rule target contains the full protocol and domain name.
Escape the literal period in index\.php
Unless you want the original query string to be re-appended to the new URL add a question mark to the end of the target URL.
| 9:27 pm on Apr 4, 2012 (gmt 0)|
|I have redirects and rewrite rules in multiple htaccess files throughout different directories. The redirect rules work but I can't get the rewrites to work outside of the root level htaccess. |
That's not a Rewrite, it's a Redirect. When you say you have both, do you mean that you have both
RewriteRule blahblah [L]
RewriteRule blahblah [R=301,L]
or do you mean you have both
Redirect 301 blahblah
If it's the second, prepare for a chorus of Danger! Danger!
| 4:12 am on Apr 5, 2012 (gmt 0)|
g1smd, thanks for the tips! I was wondering about the query at the end.
lucy24, change my redirect 301s to RewriteRule blahblah [R=301,L] ? Why would this cause problems the way I have it? I've been good so far combining the two.
| 4:44 am on Apr 5, 2012 (gmt 0)|
Within any given module, commands happen in the order you wrote them. (We're talking redirects here, not core-level "Order Allow,Deny" structures which have their own rules.) But the order of the modules themselves is fixed: all of mod_a, all of mod_b, all of mod_c and so on, no matter what order your htaccess has them in. Even if they're all mixed up, the system handles them one at a time.
Unless it's your own server, you have no control over what executes first, mod_alias or mod_rewrite. You can work it out experimentally-- but then your host goes and upgrades the installation, things come out in a different order, and there go all your Redirects down the tubes. Suddenly your rewritten requests are being exposed to mod_alias redirects when you'd assumed all of that was safely out of the way.
So it's more of a potential danger than an absolute danger. I used both formats myself for several years without collision-- but I never had any Rewrites as such, and the different kinds of rules affected entirely different directories. That is, any given request might meet mod_alias or it might meet mod_rewrite but it could never meet both.
Apache's own docs say that you should never use mod_rewrite when mod_alias will do-- as for a simple, conditionless redirect. But I think they're assuming it's your own installation so you've got complete control over what happens where and in what order.
| 7:03 pm on Sep 13, 2012 (gmt 0)|
Not sure, but I may have finally run into that problem. So I am trying to switch all Redirect 301s to
RewriteRule /example.html /products/example/ [R=301,L]
Is that the right way to do it? It doesn't seem to work. Any thoughts?
| 7:10 pm on Sep 13, 2012 (gmt 0)|
Answering my own question, this works:
RewriteRule ^example\.html? /products/example/ [R=301,L]
Edit: I've switched over to all RewriteRule just in case! :)
| 9:05 pm on Sep 13, 2012 (gmt 0)|
|I am trying to switch all Redirect 301s |
If you've got a whole lot of them, paste them into a text editor that does Regular Expressions and run the following (three separate changes, use the ones you need):
# change . to \.
^(Redirect \d\d\d \S+?[^\\])\.
# now change Redirect to Rewrite
^Redirect(?:Match)? 301 /(.+)
RewriteRule $1 [R=301,L]
^Redirect(?:Match)? 410 /(.+)
RewriteRule $1 - [G,L]
| 9:16 pm on Sep 15, 2012 (gmt 0)|
|Answering my own question, this works: |
RewriteRule ^example\.html? /products/example/ [R=301,L]
you should redirect to a fully qualified url specifying the canonical protocol and hostname:
RewriteRule ^example\.html? http://example.com/products/example/ [R=301,L]
(or www.example.com if that is the case)
| 9:22 pm on Sep 15, 2012 (gmt 0)|
why's that? is there a benefit?
| 9:34 pm on Sep 15, 2012 (gmt 0)|
it redirects the path and canonicalizes the hostname in one hop.
otherwise you are allowing your redirected path to be served from a non-canonical hostname or it requires 2 redirects to get there.
| 9:50 pm on Sep 15, 2012 (gmt 0)|
Without it, requests for
http://example.com/somepage.html will be redirected to
and requests for
http://www.example.com/somepage.html will be redirected to
Your canonical non-www/www redirect rule will then kick in to ensure that non-canonical requests always pass through an unwanted multiple step redirection chain.
So, arrange the rules in this order:
- all redirects first, all rewrites last, and
- within each group, order from most specific to most general.
Redirects should always have the full protocol and hostname. This ensures that whatever is requested, one and only one rule is triggered and this fixes all of the problems that this request has.
In another thread, there's some code with rules ordered like this:
- strip "index.php" redirecting to folder on www
- strip ".php" redirecting to extensionless on www
- redirect (all other) non-www to www.
You can work out for yourself the disaster that will ensue should someone erroneously decide to use a different order, and then request various non-www URLs.
| 2:52 am on Sep 16, 2012 (gmt 0)|
Cool, I will definitely do that. Glad to have some understanding behind it too! Thanks!