Reputation: 7715
I have a question about using multiple .htaccess
files - I couldn't find the answer to this after looking elsewhere on stackoverflow, so I hope you guys can help.
I currently have one .htaccess
file in the root of my site, which performs a simple url rewrite:
Options -MultiViews
# CheckSpelling off
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ index.php?url=$1 [L]
ErrorDocument 404 /index.php
I'm currently working on the second phase of development of this site, and I've made a replica in a subfolder (e.g. www.abcdef.com/new/). The trouble is, at the moment if I click a link on this replica site, it redirects me to the root, original page, whereas I want it to go to the equivalent page in the new/ folder. I've put another .htaccess
file in this new/ folder, which however doesn't have any noticeable effect:
Options -MultiViews
# CheckSpelling off
RewriteEngine On
RewriteBase /new/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ /new/index.php?url=$1 [L]
ErrorDocument 404 /index.php
So my question is: is it permissible to have another .htaccess
file in a subfolder like this? And if so, why aren't the above lines working?
Thanks in advance for any ideas on this!
Upvotes: 1
Views: 13936
Reputation: 8949
You say
The trouble is, at the moment if I click a link on this replica site, it redirects me to the root, original page, whereas I want it to go to the equivalent page in the new/ folder.
Could it be that you are using absolute links in your pages and not relative ones? For instance if a link looks like "/sample"
, when in your main site it will link to http://.../sample
and the same is true if the link is inside a page under "/new/"
. If you'd use just "sample"
then that would resolve as http://..../sample
or http://...../new/sample
, depending on the URL of the page.
Upvotes: 0
Reputation: 29208
It's possible to have multiple .htaccess files, and the system is designed to work the way you want it to.
You're setting RewriteBase
, which explicitly sets the base URL-path (not filesystem directory path!) for per-directory rewrites.
So it seems like your requests would be rewritten to /new/new/index.php
, a path and directory which probably doesn't exist on your filesystem (thus not meeting your RewriteCond
s) and such is being redirected to your /index.php 404.
As a test, perhaps try changing the ErrorDocument
to:
ErrorDocument
404 /new/index.php
If you see rewritten calls go to this then it might indeed be your RewriteBase
.
Upvotes: 2
Reputation: 154454
Having a second htaccess file in a subdirectory shouldn't be an issue, and as far as I can tell, your two look okay.
Are you sure the links in the site are correct? (ex, they are /new/foo
, not just /foo
)?
Upvotes: 0