Erik
Erik

Reputation: 227

URL rewrite not working when another htaccess is in a subfolder

I have an .htaccess file in the root of my site. Inside the file, I have a code that redirects all traffic to add "www." to the beginning of the url:

Options +FollowSymLinks 
RewriteEngine on 
RewriteCond %{HTTP_HOST} ^website.com [NC] 
RewriteRule ^(.*)$ http://www.website.com/$1 [L,R=301]

This works great for 95% of the site. However, I have one subdirectory, website.com/playdate, that has another .htaccess file inside the folder. Inside this .htaccess file is the following code, to redirect the URL to something a little cleaner:

Options +FollowSymLinks
RewriteEngine on
RewriteRule ^page/([0-9]+)/ index.php?page=$1

Options +FollowSymLinks
RewriteEngine on
RewriteRule ^([a-zA-Z0-9_-]+)/ view.php?url_slug=$1

It seems like this code is overriding the www redirect. I have tried to add the initial code,

Options +FollowSymLinks 
RewriteEngine on 
RewriteCond %{HTTP_HOST} ^website.com [NC] 
RewriteRule ^(.*)$ http://www.website.com/$1 [L,R=301]

to the top of the secondary .htaccess, but this does not work.

How do I get the subdirectory to continue to forward to the "www." version of the site, while also keeping the secondary .htaccess rules for this subfolder?

Upvotes: 2

Views: 1886

Answers (2)

user3063437
user3063437

Reputation: 1

I think your problem is the L flag you are using when you rewrite the full domain name.

[L,R=301]

The L flag means "Last", as in, "this is the last rewrite directive I want the server to process for this URI request." If the user agent requests "http://website.com/playdate", then the rewrite condition will trigger the line

RewriteRule ^(.*)$ http://www.website.com/$1 [L,R=301]

The server will rewrite the request to http://www.website.com/playdate and stop processing rewrite rules for that URI request because the server comes to the L flag.

At least that is my guess.

Upvotes: 0

Jon Lin
Jon Lin

Reputation: 143886

When a request is made, for example /playdate, the first thing that happens is a URL to file mapping is made, then apache checks if that file mapping, which in this case maps to a directory, contains an htaccess file. If it doesn't, then it checks parent directories for htaccess files. If it does, then it simply uses the htaccess file in that directory.

So when a request is made for /playdate, it has an htaccess file in that directory so apache doesn't bother looking for on in any parent directories. Only this one htaccess file is applied, any ones sitting in parent directories are ignored.

If you are using apache 2.2, then you'll just have to copy over the www redirect rules, but you'll need to tweak them because your no longer in the document root. Add these rules to the top of your htaccess file (or at least, above any rules that you already have in there):

RewriteEngine on 
RewriteCond %{HTTP_HOST} ^website.com [NC] 
RewriteRule ^(.*)$ http://www.website.com/playdate/$1 [L,R=301]

If you are using apache 2.4, there is a specific option you can apply to the htaccess file in the playdate directory:

RewriteOptions InheritBefore

This makes it so any rules in the parent scope are inherited before any rules in the child scope. This is important because you need to make sure the www redirect rules occur before any of your routing rules are applied, otherwise the URI gets mangled. Apache 2.2 has a similar option, Inherit but those are placed after any rules in the child scope.

Upvotes: 2

Related Questions