exclude sub directory from htaccess rules in root folder - php

I am using below htaccess rules on my website's root folder to remove the files extensions and add trailing slash at the end. However, I'd like to exclude a sub-directory /sub-dir/ from the root folder htaccess rules. I tried to add another htaccess to the /sub-dir/ folder and put RewriteOptions inherit or RewriteEngine Off in the file but none of them works.
RewriteEngine On
RewriteBase /
# redirect from non-www to www
RewriteCond %{HTTP_HOST} ^mywebsite.com.au$ [NC]
RewriteRule ^(.*)$ http://www.mywebsite.com.au/$1 [R=301,L]
RewriteCond %{REQUEST_URI} ^/index.php$
RewriteRule ^(.*)index.php$ http://www.mywebsite.com.au/$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{THE_REQUEST} ^GET\ /[^?\s]+\.php
RewriteRule (.*)\.php$ /$1/ [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*)/$ $1.php [L]
# Remove trailing slash:
RewriteRule (.*)/$ $1 [L]
# Now test without the trailing slash:
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule . %{REQUEST_FILENAME}.php [QSA,L]
Any suggestion is highly appreciated.
Thanks

You can either try adding an htaccess file in your subdirectory that has only this:
RewriteEngine On
RewriteRule ^ - [L]
Without any inherit options. Inherit means you want the rules in the parent htaccess to also be applied along with any rules in the subdirectory's htaccess file. With this, you're making sure the rewrite engine is on and making a "do nothing" pass-through rule.
Otherwise, you can add the passthrough at the top of the root's htaccess file (right below RewriteBase):
RewriteRule ^sub-dir/ - [L]

Related

Exclude sub-directories from mod_rewrite rule [duplicate]

I have 8 lines of rewrite rules in my .htaccess file. I need to exclude two physical directories on my server from these rules, so they can become accessible. For now all requests are sent to index.php file.
Directories to exclude: "admin" and "user".
So http requests: http://www.domain.com/admin/ should not be passed to index.php file.
ErrorDocument 404 /index.php?mod=error404
Options FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !^www\.domain\.com$ [NC]
RewriteRule ^(.*)$ http://www.domain.com/$1 [R=301,L]
RewriteRule ^([^/] )/([^/] )\.html$ index.php?lang=$1&mod=$2 [L]
RewriteRule ^([^/] )/$ index.php?lang=$1&mod=home [L]
Try this rule before your other rules:
RewriteRule ^(admin|user)($|/) - [L]
This will end the rewriting process.
What you could also do is put a .htaccess file containing
RewriteEngine Off
In the folders you want to exclude from being rewritten (by the rules in a .htaccess file that's higher up in the tree). Simple but effective.
add a condition to check for the admin directory, something like:
RewriteCond %{REQUEST_URI} !^/?(admin|user)/
RewriteRule ^([^/] )/([^/] )\.html$ index.php?lang=$1&mod=$2 [L]
RewriteCond %{REQUEST_URI} !^/?(admin|user)/
RewriteRule ^([^/] )/$ index.php?lang=$1&mod=home [L]
If you want to remove a particular directory from the rule (meaning, you want to remove the directory foo) ,you can use :
RewriteEngine on
RewriteCond %{REQUEST_URI} !^/foo/$
RewriteRule !index\.php$ /index.php [L]
The rewriteRule above will rewrite all requestes to /index.php excluding requests for /foo/ .
To exclude all existent directries, you will need to use the following condition above your rule :
RewriteCond %{REQUEST_FILENAME} !-d
the following rule
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule !index\.php$ /index.php [L]
rewrites everything (except directries) to /index.php .
We used the following mod_rewrite rule:
RewriteEngine on
RewriteCond %{REQUEST_URI} !^/test/
RewriteCond %{REQUEST_URI} !^/my-folder/
RewriteRule (.*) http://www.newdomain.com/$1 [R=301,L]
This redirects (permanently with a 301 redirect) all traffic to the site to http://www.newdomain.com, except requests to resources in the /test and /my-folder directories. We transfer the user to the exact resource they requested by using the (.*) capture group and then including $1 in the new URL. Mind the spaces.
RewriteEngine On
RewriteRule ^(wordpress)($|/) - [L]

.htaccess pass request on for /UA/ folder

This is similar to question mod_rewrite: Being redirected to root folder; I want it to stay in sub-folder
but the answer there is not working for me.
I have a main site which redirects via an index.php handler, but I want a pre-live test area which is held in a subdirectory /UA/
I need requests for UA/ to pass through and be handled in the sub directory, I have tried both RewriteRule ^/UA - [L] and RewriteCond %{REQUEST_URI} !^/UA in my .htaccess of the main directory but this hasn't worked. I've tried with/without /'s
Here are my two .htaccess files:
RewriteEngine on
ReWriteBase /
# Redirect to HTTPS site
RewriteCond %{HTTP_HOST} ^my\-site\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.my\-site\.com$
RewriteRule ^/?$ "https\:\/\/www\.mysite\.com\/" [R=301,L]
RewriteCond %{HTTP_HOST} ^mysite\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.mysite\.com$
RewriteRule ^/?$ "https\:\/\/www\.mysite\.com\/" [R=301,L]
#Allow UA access
RewriteRule ^/UA - [L]
#Allow certain file types to access directly
RewriteRule \.(css|js|png|jpg|gif|woff|eot|ttf|svg|ico)$ - [L]
# Catch all
RewriteCond %{REQUEST_URI} !^/UA
RewriteRule .* index.php [L]
RewriteEngine on
ReWriteBase /UA/
#Allow certain file types to access directly
RewriteRule \.(css|js|png|jpg|gif|woff|eot|ttf|svg|ico)$ - [L]
# Catch all
RewriteRule .* index.php [L]
The problem is that the site continues to direct to the Live index.php version not the one in /UA/ with a URI of https://www.mysite/UA/
Add this line to always have directory slash on in your root .htaccess:
DirectorySlash On
Keep /UA/.htaccess like this:
RewriteEngine on
ReWriteBase /UA/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !\.(css|js|png|jpg|gif|woff|eot|ttf|svg|ico)$ [NC]
RewriteRule ^ index.php [L]

htaccess - Exclude admin folder rewrite rule [duplicate]

I have 8 lines of rewrite rules in my .htaccess file. I need to exclude two physical directories on my server from these rules, so they can become accessible. For now all requests are sent to index.php file.
Directories to exclude: "admin" and "user".
So http requests: http://www.domain.com/admin/ should not be passed to index.php file.
ErrorDocument 404 /index.php?mod=error404
Options FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !^www\.domain\.com$ [NC]
RewriteRule ^(.*)$ http://www.domain.com/$1 [R=301,L]
RewriteRule ^([^/] )/([^/] )\.html$ index.php?lang=$1&mod=$2 [L]
RewriteRule ^([^/] )/$ index.php?lang=$1&mod=home [L]
Try this rule before your other rules:
RewriteRule ^(admin|user)($|/) - [L]
This will end the rewriting process.
What you could also do is put a .htaccess file containing
RewriteEngine Off
In the folders you want to exclude from being rewritten (by the rules in a .htaccess file that's higher up in the tree). Simple but effective.
add a condition to check for the admin directory, something like:
RewriteCond %{REQUEST_URI} !^/?(admin|user)/
RewriteRule ^([^/] )/([^/] )\.html$ index.php?lang=$1&mod=$2 [L]
RewriteCond %{REQUEST_URI} !^/?(admin|user)/
RewriteRule ^([^/] )/$ index.php?lang=$1&mod=home [L]
If you want to remove a particular directory from the rule (meaning, you want to remove the directory foo) ,you can use :
RewriteEngine on
RewriteCond %{REQUEST_URI} !^/foo/$
RewriteRule !index\.php$ /index.php [L]
The rewriteRule above will rewrite all requestes to /index.php excluding requests for /foo/ .
To exclude all existent directries, you will need to use the following condition above your rule :
RewriteCond %{REQUEST_FILENAME} !-d
the following rule
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule !index\.php$ /index.php [L]
rewrites everything (except directries) to /index.php .
We used the following mod_rewrite rule:
RewriteEngine on
RewriteCond %{REQUEST_URI} !^/test/
RewriteCond %{REQUEST_URI} !^/my-folder/
RewriteRule (.*) http://www.newdomain.com/$1 [R=301,L]
This redirects (permanently with a 301 redirect) all traffic to the site to http://www.newdomain.com, except requests to resources in the /test and /my-folder directories. We transfer the user to the exact resource they requested by using the (.*) capture group and then including $1 in the new URL. Mind the spaces.
RewriteEngine On
RewriteRule ^(wordpress)($|/) - [L]

.htaccess remove .php and force trailing slash

I have files in my root folder. jobs.php, contact.php.
I want to be able to use .htaccess to remove the .php extension and force a trailing slash /. So that, www.domain.com/jobs/ and www.domain.com/contact/ goes to their respective pages.
Also, I want jobs.domain.com to point to www.domain.com/jobs/
<IfModule mod_rewrite.c>
Options +SymLinksIfOwnerMatch
RewriteEngine On
RewriteBase /
RewriteRule ^(rosquillos)/$ $1 [L,R=301,NC]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
</IfModule>
This is what I've got so far, and as much as I try. I can't seem to get it to work.
Any help would be awesome.
EDIT:
I would like to rephrase my question. The existing code removes the .php extensions for me, what I really only need right now is an additional rule that points the subdomain to the correct file: ie. jobs.domain.com points to jobs.php. I can live without the trailing slash.
Your condition:
RewriteCond %{REQUEST_FILENAME}\.php -f
will fail if your URL's end with a trailing slash. But if you only have jobs and contact then you'll just want:
RewriteEngine On
# add trailing slash and redirect browser
RewriteRule ^([^/]+)$ /$1/ [L,R=301]
RewriteRule ^([^/]+)/$ /$1.php [L]
# redirect direct access to php files:
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^/]+)\.php
RewriteRule ^ /%1/ [L,R=301]
Oh and I forgot:
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} ^([^.]+)\.domain\.com$ [NC]
RewriteCond %{DOCUMENT_ROOT}/%1.php -f
RewriteRule ^ /%1.php [L]
Assuming that *.domain.com has the same document root as www.domain.com.

.htaccess - Redirect subfolder to index.php using subfolder name as variable

This is driving me mad. I'm trying to use .htaccess to redirect a subfolder (that doesn't exist) to the index page, using the subfolder name as the variable. ie:
http://www.website.com/john/
redirects to:
http://www.website.com/index.php?name=john
I've tried this (and various others) with no luck:
Options +FollowSymLinks
RewriteEngine On
RewriteRule ^(.*)$ index.php?name=$1
Here is an example, how you can do this:
# turn mod_rewrite engine on
RewriteEngine On
# rewrite a physical existing file or folder
RewriteCond %{REQUEST_FILENAME} !-f [OR]
RewriteCond %{REQUEST_FILENAME} !-d
# allow things that are certainly necessary
RewriteCond %{REQUEST_URI} "/layout/" [OR]
RewriteCond %{REQUEST_URI} "/javascript/"
# rewrite rules
RewriteRule .* - [L]
RewriteRule (.*) index.php?_route=$1 [QSA]
This one also denies access to folders you don't want to have public.
Try this one:
RewriteRule ^([^/]*)/$ /?name=$1 [L]
RewriteRule ^([^.]*)$ /index.php?name=$1 [L,QSA]

Categories