Apache rewrite conditions not catching existing directories or files - apache

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/board/([a-zA-Z0-9\_\-]*).* /board/index.php?$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/board/user/([a-zA-Z0-9\_\-]*).* /board/user/index.php?$1 [L]
I am trying to forward mydomain.com/board/something to mydomain.com/board/index.php?something and /board/user/something to /board/user/index.php?something. And have it not rewrite existing files or directories.
The board/ one works but it fires even if i request an existing directory so I can't test the second one. Also a page like mydomain.com/board/signup/index.php is forwarding to /board/index.php?signup
These are all within a <VirtualHost *:443> and my VPS is actually handling a few different domain names on the same instance if that makes a difference.
I found similar issues in search but their solution was what I am using above already.

Apparently on Apache 2.4 (maybe 2.2 and above?) you have to do it like this:
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_FILENAME} !-f
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_FILENAME} !-d
However this does not catch if you navigate to, say, mydomain.com/board/ when there is an index.php there. So this is what I have come up with, finally.
RewriteEngine On
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_FILENAME} !-f
RewriteCond $1 !index\.
RewriteCond %{DOCUMENT_ROOT}/board/$1/index.php !-f
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_FILENAME} !-d
RewriteRule ^/board/([a-zA-Z0-9\_\-\.]*) /board/index.php?$1 [L]
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_FILENAME} !-f
RewriteCond $1 !index\.
RewriteCond %{DOCUMENT_ROOT}/board/user/$1/index.php !-f
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_FILENAME} !-d
RewriteRule ^/board/user/([a-zA-Z0-9\_\-\.]*) /board/user/index.php?$1 [L]

Related

Apache Redirect from a folder to a index.php file

I want to redirect from http://www.domain.com/customer1 to /app/index.php?c=customer1 and http://www.domain.com/customer2 to /app/index.php?c=customer2
I tried the below rule but not works well...
RewriteRule ^customer1(.*)$ /app/index.php?c=customer1
I am assuming you are talking about rewriting,
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([\w-]+)$ /app/index.php?c=$1 [QSA,L]
This will internally rewrite your urls if not rewriting then try it like this,
RewriteEngine on
RerwiteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([\w-]+)$ /app/index.php?c=$1 [R=301,QSA,L]

Apache .htaccess to redirect all URLs except for files and directories

I am developing a web site currently located at a subdomain. In the .htaccess file I have the following:
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^([a-zA-Z_\-]+)/?$ /index.php?url=$1 [L,NC]
RewriteRule ^([a-zA-Z_\-]+)/(.+)/?$ /index.php?url=$1&$2 [L,NC]
When I try to load an existing css file e.g. http://subdomain/path/to/existing/file.css Apache still redirects it to index.php. Is it possible to redirect all URLs to index.php except for existing files and directories?
The problem with your code is that the last rule is not conditioned, as you can only have one RewriteRule per condition or set thereof.
There's more than likely a better way to do this (perhaps using the skip flag S), but you can repeat your conditions in the meantime.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^([a-z_-]+)/?$ index.php?url=$1 [L,NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^([a-z_-]+)/(.+)/?$ index.php?url=$1&$2 [L,NC]
Also, and as you have specified the NC flag, you need not specify A-Z in the pattern. Also, no need to escape the hyphen -.
Update: You could also try setting an environment variable, per this answer:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
# If the conditions above are true, then set a variable to say so
# and continue.
RewriteRule ^ - [E=TRUE:YES]
RewriteCond %{ENV:TRUE} = YES
RewriteRule ^([a-zA-Z_\-]+)/?$ /index.php?url=$1 [L,NC]
RewriteCond %{ENV:TRUE} = YES
RewriteRule ^([a-zA-Z_\-]+)/(.+)/?$ /index.php?url=$1&$2 [L,NC]

Pretty URLs .htaccess

I always get in a tangle with .htaccess so was hoping someone could help me write one.
I have found stuff online similar to what I want, but I'm not confident enough
to rewrite their rewrites :P
I want this:
/foo/ = index.php?a=foo
/foo/bar/ = index.php?a=foo&b=bar
etc. up to /foo/bar/baz/cat/dog/ (&e=dog)
I also want the index.php to be invisible so that /index.php rewrites to /.
Another thing, I would like there to always be a trailing slash and therefore the ability
to do...
/foo/bar/baz/?another=whatever
I have directories such as /images/ that I don't want this to apply to,
so would I have to make a white list for this or are there certain redirects I can use?
Cheers!
You don't need to whitelist directories. That's what RewriteConds are for.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
It checks to make sure it's not a real file or real directory then performs the RewriteRule.
Try these rules below in your .htaccess file to get what you want.
RewriteEngine On
RewriteCond %{REQUEST_URI} /+[^\.]+$
RewriteRule ^(.+[^/])$ %{REQUEST_URI}/ [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/([^/]+)/([^/]+) index.php?a=$1&b=$2&c=$3&d=$4&e=$5&%{QUERY_STRING} [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/([^/]+) index.php?a=$1&b=$2&c=$3&d=$4&%{QUERY_STRING} [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^/]+)/([^/]+) index.php?a=$1&b=$2&c=$3&%{QUERY_STRING} [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^/]+) index.php?a=$1&b=$2&%{QUERY_STRING} [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+) index.php?a=$1&%{QUERY_STRING} [L]

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]