homepage Welcome to WebmasterWorld Guest from 54.198.148.191
register, free tools, login, search, subscribe, help, library, announcements, recent posts, open posts,
Pubcon Platinum Sponsor
Home / Forums Index / Code, Content, and Presentation / Apache Web Server
Forum Library, Charter, Moderators: Ocean10000 & incrediBILL & phranque

Apache Web Server Forum

    
Making Multiple .htaccess files
rwilson




msg:4437178
 6:42 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. is there something I need to do differently?

This works when in the root level but not in the products directory.

Options +FollowSymLinks
Options -Indexes
RewriteEngine On

rewritecond %{QUERY_STRING} ^cat=1&scat=12&sku=(sample-sku1|sample-sku2|sample-sku3)$
rewriteRule ^products/index.php$ /sample-landing-page.php/ [R=301,L]

 

phranque




msg:4437189
 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:

http://httpd.apache.org/docs/2.0/mod/mod_rewrite.html#rewriterule:
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.

rwilson




msg:4437195
 7:35 pm on Apr 4, 2012 (gmt 0)

Ah, I missed that. Thanks!

g1smd




msg:4437196
 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.

lucy24




msg:4437251
 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.
<snip>
[R=301,L]

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]
and
RewriteRule blahblah [R=301,L]

or do you mean you have both
RewriteRule blahblah
and
Redirect 301 blahblah

If it's the second, prepare for a chorus of Danger! Danger!

rwilson




msg:4437331
 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.

lucy24




msg:4437335
 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.

rwilson




msg:4494849
 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

RewriteEngine On
RewriteRule /example.html /products/example/ [R=301,L]

Is that the right way to do it? It doesn't seem to work. Any thoughts?

rwilson




msg:4494851
 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! :)

lucy24




msg:4494892
 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+?[^\\])\.
REPLACE WITH
$1\\.

# now change Redirect to Rewrite

^Redirect(?:Match)? 301 /(.+)
REPLACE WITH
RewriteRule $1 [R=301,L]

# and

^Redirect(?:Match)? 410 /(.+)
REPLACE WITH
RewriteRule $1 - [G,L]

phranque




msg:4495578
 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)

rwilson




msg:4495581
 9:22 pm on Sep 15, 2012 (gmt 0)

why's that? is there a benefit?

phranque




msg:4495586
 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.

g1smd




msg:4495589
 9:50 pm on Sep 15, 2012 (gmt 0)

Without it, requests for
http://example.com/somepage.html will be redirected to http://example.com/products/somepage/
and requests for
http://www.example.com/somepage.html will be redirected to http://www.example.com/products/somepage/

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.

rwilson




msg:4495617
 2:52 am on Sep 16, 2012 (gmt 0)

Cool, I will definitely do that. Glad to have some understanding behind it too! Thanks!

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / Apache Web Server
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About
© Webmaster World 1996-2014 all rights reserved