RewriteRule - folder exceptions (with dashes) - apache

I have drupal installed in my website root with a directory called xi-admin underneath it, that directory has a .htaccess inside doing password protection. problem is, is that it is rewriting http://www.example.com/xi-admin/ back to index.php????
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^(xi-admin|xi-admin/.*)$
RewriteCond %{REQUEST_URI} !=/favicon.ico
RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]

Try changing
RewriteCond %{REQUEST_URI} !^(xi-admin|xi-admin/.*)$
to
RewriteCond %{REQUEST_URI} !^/xi-admin

In your configuration, you are referring in one rule to foo, in the other to /bar, one of them will never match.
Either choose:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^/(xi-admin|xi-admin/.*)$
RewriteCond %{REQUEST_URI} !=/favicon.ico
RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]
Or another alternative using relative addressing, which I would recommend because it works even if this directory would later be moved to somewhere else in the directory hierarchy:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond $1 !^(xi-admin|xi-admin/.*)$
RewriteCond $1 !=favicon.ico
RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]

Related

RewriteRule in .htacess to have all strings except some load one file and the other strings load another

This is my .htaccess file:
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1 !-f
RewriteCond %{REQUEST_URI} /category1|category2|category3|category4/ [NC]
RewriteRule ^([^/]+)/?$ category.php?category=$1 [L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1 !-f
RewriteCond %{REQUEST_URI} (?!category1|category2|category3|category4) [NC]
RewriteRule ^([^/]+)/?$ article.php?slugId=$1 [L]
I want to have the following links working to load the category.php file with the corresponding value for the category parameter:
website.com/category1
website.com/category2
website.com/category3
website.com/category4
And I want the rest of the strings in the format of website.com/string to load the article.php file with the string as its parameter.
Whenever I search for any of the categories it works fine. However, if I search for any other string it redirects me back to index.php
Based on your shown samples and attempts, could you please try following. Please make sure to clear your browser cache before testing your URLs.
RewriteEngine ON
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1 !-f
RewriteCond %{REQUEST_URI} (category1|category2|category3|category4) [NC]
RewriteRule ^([^/]+)/?$ category.php?category=$1 [L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1 !-f
RewriteCond %{REQUEST_URI} !(category1|category2|category3|category4) [NC]
RewriteRule ^([^/]+)/?$ article.php?slugId=$1 [L]

.htaccess rewrite to add country code (if missing)

I am using a .htaccess file in the subdirectory /cms and using this subdirectory as RewriteBase. The redirections go to 'backend.php' and send the variables I want. It is a dual language site (nl|en), dutch and english. Everything works fine, as long as the %{REQUEST_URI} starts with (nl|en).
But I need a fallback to the default dutch language when nl|en is omitted, but can I add this to the following .htacces file. I have been trying and searching but cannot find the right syntax to make this happen:
RewriteEngine On
RewriteBase /cms/
RewriteCond %{REQUEST_URI} !(/$|\.)
RewriteRule (.*) %{REQUEST_URI}/ [R=301,L]
RewriteCond %{REQUEST_URI} !(^(nl|en)/)
#RewriteRule (.*) This is where the solution should be used ?
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !\.[a-zA-Z]{2,4}$
RewriteRule ^(nl|en)/([^/]+)/$ backend.php?page=$2&language=$1 [QSA,NC,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !\.[a-zA-Z]{2,4}$
RewriteRule (nl|en)(.*)/(\d+)/$ backend.php?page=$2&id=$3&language=$1 [QSA,NC,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !\.[a-zA-Z]{2,4}$
RewriteRule (nl|en)(.*)/(\d+)/(.+)/$ backend.php?page=$2&id=$3&task=$4&language=$1 [QSA,NC,L]
One way to handle this is a rewrite with the dutch language prepended. This will silently change direction to the proper page.
RewriteCond %{REQUEST_URI} !^/cms/(?:nl|en)/
RewriteRule ^(.*)$ nl/$1 [L]
If you want the client to notice and change the URL, you must do a R|redirect instead
RewriteCond %{REQUEST_URI} !^/cms/(?:nl|en)/
RewriteRule ^(.*)$ nl/$1 [R,L]
Your default rule can be this one:
# This is where the solution should be used ?
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^/(nl|en)/ [NC]
RewriteRule ^([^/]+)/?$ backend.php?page=$1 [QSA,L]

htaccess rewrite username messing with pages

I can access profiles using mysite.com/username but I cannot access mysite.com/home which originally is mysite.com/index.php?tab1=home.
Same problem with all other pages, for example, I also cannot access mysite.com/messages which originally is mysite.com/index.php?tab1=messages
I want to access both profiles and pages using the same link. mysite.com/profile & mysite.com/page.
This is the htaccess code I used.
RewriteEngine on
RewriteCond %{REQUEST_URI} !^/index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^\/\/]*)$ index.php?tab1=profile&id=$1 [NC]
RewriteRule ^([^\/\/]*)/([^\/\/]*)(|\/)$ index.php?tab1=profile&tab2=$2&id=$1 [NC]
RewriteRule ^([^\/\/]*)/([^\/\/]*)/([^\/\/]*)(|/)$ index.php?tab1=profile&tab2=$2&tab3=$3&id=$1 [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^\/\/]*)$ index.php?tab1=$1 [NC]
You have this rule:
RewriteRule ^([^\/\/]*)$ index.php?tab1=profile&id=$1 [NC]
Which has the pattern ^([^\/\/]*)$. So this matches "username", "home", AND "messages". That's why it's always going to the profile because that rule indiscriminately matches everything without a /.
Therefore, the last rule, which has the same pattern, will never match anything. You either need to make the profile a diffrent pattern, or hardcode the matches for "home" and "messages" and everything else that doesn't go to the profile.
So something like:
RewriteEngine on
RewriteRule ^(home|messages)$ index.php?tab1=$1 [L]
RewriteCond %{REQUEST_URI} !^/index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^\/\/]*)$ index.php?tab1=profile&id=$1 [NC,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^\/\/]*)/([^\/\/]*)(|\/)$ index.php?tab1=profile&tab2=$2&id=$1 [NC,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^\/\/]*)/([^\/\/]*)/([^\/\/]*)(|/)$ index.php?tab1=profile&tab2=$2&tab3=$3&id=$1 [NC,L]
Or something like this maybe?
RewriteEngine on
RewriteRule ^profile-([^\/\/]*)$ index.php?tab1=profile&id=$1 [NC]
RewriteRule ^profile-([^\/\/]*)/([^\/\/]*)(|\/)$ index.php?tab1=profile&tab2=$2&id=$1 [NC]
RewriteRule ^profile-([^\/\/]*)/([^\/\/]*)/([^\/\/]*)(|/)$ index.php?tab1=profile&tab2=$2&tab3=$3&id=$1 [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^\/\/]*)$ index.php?tab1=$1 [NC]
Making user profiles look like: mysite.com/profile-username

Redirect to a specific subfolder using htaccess

Here's my folder structure:
website.com
main
sub1
sub2
I would like to be able to access the subfolder sub1 via the URL website.com/sub1, which I managed to do so by adding the following in my .htaccess under website/ :
RewriteCond %{REQUEST_URI} /sub1/(.*)$
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule (.*)$ main/sub1/index.php?view=$1 [QSA,L]
My problem is that although the index.php is accessed, it is accessed all the time, even for the .js and .css files. So I tried to add the following rule before (although I though I excluded the files via my !-f condition before):
RewriteCond %{REQUEST_URI} /sub1/(.*)$
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^(.*)$ main/sub1/$1 [L,QSA]
But as you can guess, no luck, no actual files ever go into that second block even when I delete the other one.
Try:
RewriteCond %{REQUEST_URI} ^/sub1/(.*)$
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{DOCUMENT_ROOT}/main/%{REQUEST_URI} -f
RewriteRule ^(.*)$ /main/$1 [L]
then your old rules:
RewriteCond %{REQUEST_URI} /sub1/(.*)$
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule (.*)$ main/sub1/index.php?view=$1 [QSA,L]

Rewrite rule to handle url exception

I have an app that serves many sites and I am using Apache mod-rewrite to map the
url's like this
http://site1.net/controller
http://site2.net/controller2/another_view
http://site3.net/
http://special_case.net/
maps to:
index.php?url=http://site1.net/controller
index.php?url=http://site2.net/controller2/another_view
index.php?url=http://site3.net/
index.php?url=http://special_case.net/hub
My rewrite rules are:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?url=$1 [QSA,L]
Which handles all the cases except the special last case, where I need to force
the use of a controller called "hub" when handling a specific domain. I collaborate with others on this project which means I can't do anything about the routing once the index file is called.
Can someone fix my rules so that all the above cases resolve?
You current rules don't seem to add the hostname to the url get-parameter. So I added that to to the following:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
RewriteCond %{HTTP_HOST} ^(special_case\.net)$
RewriteRule ^(.*)$ index.php?url=http://%1/hub/$1 [QSA,L]
RewriteCond %{HTTP_HOST} ^(.*)$
RewriteRule ^(.*)$ index.php?url=http://%1/$1 [QSA,L]
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^http://special_case.net index.php?url=http://special_case.net/hub [QSA,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?url=$1 [QSA,L]