Welcome to WebmasterWorld Guest from 54.167.159.180

Forum Moderators: Ocean10000 & incrediBILL & phranque

Message Too Old, No Replies

Making Multiple .htaccess files

     
6:42 pm on Apr 4, 2012 (gmt 0)

Junior Member

5+ Year Member

joined:Aug 17, 2010
posts: 188
votes: 2


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]
7:10 pm on Apr 4, 2012 (gmt 0)

Administrator

WebmasterWorld Administrator phranque is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:Aug 10, 2004
posts:10683
votes: 33


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.
7:35 pm on Apr 4, 2012 (gmt 0)

Junior Member

5+ Year Member

joined:Aug 17, 2010
posts: 188
votes: 2


Ah, I missed that. Thanks!
7:39 pm on Apr 4, 2012 (gmt 0)

Senior Member

WebmasterWorld Senior Member g1smd is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:July 3, 2002
posts:18903
votes: 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)

Senior Member from US 

WebmasterWorld Senior Member lucy24 is a WebmasterWorld Top Contributor of All Time 5+ Year Member Top Contributors Of The Month

joined:Apr 9, 2011
posts:13439
votes: 390


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!
4:12 am on Apr 5, 2012 (gmt 0)

Junior Member

5+ Year Member

joined:Aug 17, 2010
posts: 188
votes: 2


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)

Senior Member from US 

WebmasterWorld Senior Member lucy24 is a WebmasterWorld Top Contributor of All Time 5+ Year Member Top Contributors Of The Month

joined:Apr 9, 2011
posts:13439
votes: 390


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 Sept 13, 2012 (gmt 0)

Junior Member

5+ Year Member

joined:Aug 17, 2010
posts: 188
votes: 2


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?
7:10 pm on Sept 13, 2012 (gmt 0)

Junior Member

5+ Year Member

joined:Aug 17, 2010
posts: 188
votes: 2


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 Sept 13, 2012 (gmt 0)

Senior Member from US 

WebmasterWorld Senior Member lucy24 is a WebmasterWorld Top Contributor of All Time 5+ Year Member Top Contributors Of The Month

joined:Apr 9, 2011
posts:13439
votes: 390


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]
9:16 pm on Sept 15, 2012 (gmt 0)

Administrator

WebmasterWorld Administrator phranque is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:Aug 10, 2004
posts:10683
votes: 33


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 Sept 15, 2012 (gmt 0)

Junior Member

5+ Year Member

joined:Aug 17, 2010
posts: 188
votes: 2


why's that? is there a benefit?
9:34 pm on Sept 15, 2012 (gmt 0)

Administrator

WebmasterWorld Administrator phranque is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:Aug 10, 2004
posts:10683
votes: 33


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 Sept 15, 2012 (gmt 0)

Senior Member

WebmasterWorld Senior Member g1smd is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:July 3, 2002
posts:18903
votes: 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.
2:52 am on Sept 16, 2012 (gmt 0)

Junior Member

5+ Year Member

joined:Aug 17, 2010
posts: 188
votes: 2


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

Join The Conversation

Moderators and Top Contributors

Hot Threads This Week

Featured Threads

Free SEO Tools

Hire Expert Members