Reputation: 23
I'm currently using this to make my URLs look pretty:
ErrorDocument 403 /404.php
ErrorDocument 404 /404.php
Options -Indexes
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}/index.html !-f
RewriteCond %{REQUEST_FILENAME}/index.php !-f
RewriteRule . index.php [L]
</IfModule>
I finally got it to work (I'm very new to this), but now I need this to work for a 'subsite' that is sitting in a subdirectory of the mainsite. Instead of rewriting https://example.com/index.php it needs to rewrite https://example.com/sub/index.php
I tried some things like changing the RewriteBase and RewriteRule, but with no succes.
Just to be sure, I should be able to put another .htaccess in a subdirectory and expect it to rewrite the urls for example.com/sub, right?
Thanks in advance!
EDIT I added exceptions to the main .htaccess like so: ErrorDocument 403 /404.php ErrorDocument 404 /404.php Options -Indexes
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !^(/controle|/controle/|/voting|/voting/.*)$
RewriteCond %{REQUEST_FILENAME}/index.html !-f
RewriteCond %{REQUEST_FILENAME}/index.php !-f
RewriteRule . index.php [L]
</IfModule>
However, I still do not know what to put into the .htaccess in the subdir, the same one does not work and I tried changing things like
RewriteBase /
to RewriteBase /subdir1/
Upvotes: 2
Views: 1770
Reputation: 45829
Just to be sure, I should be able to put another
.htaccess
in a subdirectory and expect it to rewrite the urls forexample.com/sub
, right?
Yes. The mod_rewrite directives in the /subdir/.htaccess
file will, by default, completely override the mod_rewrite directives in the parent/root .htaccess
file. However, you will need to redefine the ErrorDocument
directives in order to override the root.
RewriteCond %{REQUEST_FILENAME}/index.html !-f RewriteCond %{REQUEST_FILENAME}/index.php !-f
To clarify, with these two conditions are you wanting to occasionally serve the index.html
or index.php
(DirectoryIndex) document from a requested subdirectory, instead of routing the request to /index.php
in the document root? In this case it would be more common (and marginally more optimal) to simply check that the request does not map to a directory instead (ie. RewriteCond %{REQUEST_FILENAME} !-d
). Unless there are some physical subdirectories that you want to route to /index.php
in the document root?
<IfModule mod_rewrite.c> RewriteEngine On RewriteBase / RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME}/index.html !-f RewriteCond %{REQUEST_FILENAME}/index.php !-f RewriteRule . index.php [L] </IfModule>
These directives could be simplified slightly - and this will help you if you want to apply the same directives to the subsite in /subdir
(and serve /subdir/index.php
instead).
Remove the RewriteBase
directive entirely. The relative susbtitution string, ie. index.php
, will then be relative to the directory where the .htaccess
file is located (or inherited).
You also don't need the <IfModule mod_rewrite.c>
wrapper. See my answer to the following question on the Webmasters Stack for more information on this: https://webmasters.stackexchange.com/questions/112600/is-checking-for-mod-write-really-necessary
So, the above can be written:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}/index.html !-f
RewriteCond %{REQUEST_FILENAME}/index.php !-f
RewriteRule . index.php [L]
(And note my comment above about replacing the two conditions with a directory check instead.)
Now, you could simply copy the exact same directives into the /subdir/.htaccess
file and these will now route requests to /subdir/index.php
instead, overriding the mod_rewrite directives in the parent.
No need to copy Options -Indexes
, unless you want to change this option.
OR, if you want to apply the exact same directives (but in relation to the /subdir
) you could simply enable mod_rewrite inheritance in the /subdir/.htaccess
file. For example:
# /subdir/.htaccess
ErrorDocument 403 /subdir/404.php
ErrorDocument 404 /subdir/404.php
RewriteEngine On
RewriteOptions Inherit
The effect of RewriteOptions Inherit
is to essentially "copy" the mod_rewrite directives from the parent config, ie. /.htaccess
in the root, behind the directives in the current /subdir/.htaccess
file. It is important to clarify that the directives are "copied", they are not run in-place as if in the root /.htaccess
file.
Upvotes: 2