mod_rewrite: Excluding subdirectory from RewriteRule - mod-rewrite

The following rewrite Rule in .htaccess does not work as expected. It does not exclude /FileShare subdirectory from the rewrite rule although it should:
RewriteEngine on
RewriteCond %{REQUEST_URI} /([0-9]+)
RewriteCond %{REQUEST_URI} !/FileShare
RewriteRule ([0-9]+) Router.php?id=$1 [QSA]
# Still call existing files (for css, gfx etc.)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# Redirect every other file call
RewriteRule ^.*$ Router.php [QSA]
# Redirect every other call even without file name in it
RewriteRule ^$ Router.php [QSA]
Desired outcome is
/Command/Method -> Router.php
/Command/Method/123 -> Router.php?id=123
/FileShare/Command/Method -> Router.php
/FileShare/Command/Method/123 -> Router.php (without stripping 123 and without id=123)
Your help is much appreciated

It seems as if the above script does work. The problem was that Router.php did not react correctly.

Related

Why isn't this mod_rewrite re-directing?

I'm sending every request through an index.php except for pages in my blog subdirectory. I've been able to do this using mod_rewrite in my parent folder and;
RewriteCond %{REQUEST_URI} !^/blog
RewriteRule ^(.+)$ index.php?url=$1 [QSA,L]
]
However, I'd also like to send requests to my blog folder if they have the form:
documentation/some-file.
I've tried:
RewriteRule ^/documentation/(.+)$ https://www.some_domain.com/blog/documentation/$1
but it looks like my request isn't getting sent to the blog folder in this case. My full code is below:
RewriteEngine On
#redirect to index.php as appropriate
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^/documentation/(.+)$ https://www.some_domain.com/blog/documentation/$1
RewriteCond %{REQUEST_URI} !^/blog
RewriteRule ^(.+)$ index.php?url=$1 [QSA,L]
Edit:
I've used a slightly modified version of #Rijul's suggestion below and after moving the RewriteRule to before the RewriteCond, it works as I had hoped. In other words, the re-write for documentation performs the re-write to the blog subfolder. And, all other requests go through my index.php file. At this point, I would like to understand why.
RewriteEngine On
RewriteRule ^documentation/?(.*)$ /blog/documentation/$1 [R=301,L]
#redirect to index.php as appropriate
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(.+)$ index.php?url=$1 [QSA,L]
From what i know
RewriteEngine On
Without this all the rewrite rules and condition will be ignore
RewriteRule ^documentation/?(.*)$ /blog/documentation/$1 [R=301,L]
Rewrites documentation to blog/documentation
RewriteCond %{REQUEST_FILENAME} !-d
This rewrite condition checks whether the requested directory name doesn't exits. If it doesn't exits. Then proceeds to the next condition
RewriteCond %{REQUEST_FILENAME} !-f
This rewrite condition checks whether the requested file name doesn't exits. If it doesn't exits. Then proceeds to the next condition
RewriteCond %{REQUEST_FILENAME} !-l
This rewrite condition checks whether the requested symbolic link doesn't exits. If it doesn't exits. Then proceeds to rewrite rule
RewriteRule ^(.+)$ index.php?url=$1 [QSA,L]
Rewrite to index.php?url=
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
writing rewrite condition like this produces an and operation.
(no file exits in that name) and (no directory exits in that name) and
(no symbolic link exits in that name)
If this is true then rewrite to php file. (no directory exits in that name) will go false in the case for /blog (since such a directory exits)

How to redirect all pages only to index.html using htaccess file and not redirect the image files

How to redirect all pages (pages only) to index.html using htaccess file and not redirect the image files. For some reason I am using this code and an image file on the index.html page isn't showing up.
RewriteEngine on
RewriteCond %{REQUEST_URI} !^/index.html$
RewriteRule .* /index.html [L,R=302]
Try this code :
RewriteEngine on
RewriteCond %{REQUEST_URI} !^/index.html$
RewriteCond %{REQUEST_URI} !\.(gif|jpe?g|png|css|js)$
RewriteRule .* /index.html [L,R=302]
Keep your rules simple. Instead of filtering what shouldn't match, just match on the files.
RewriteEngine on
RewriteCond %{REQUEST_URI} !^/index.html$
RewriteRule .*\.(php|html)$ /index.html [L,R=302]
A nicer way might be just to ignore existing files. For example:
RewriteEngine on
RewriteCond %{REQUEST_URI} !^/$
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule .* / [L,R=302]
The !-d says to ignore the rewrite if there is an existing directory that meets the match.
The !-f says to ignore the rewrite if there is an existing file that meets the match.
Everything else will get rewritten.
You need to add a .htaccess file inside the public directory, so whenever you build your app, it automatically gets copied to the new dist directory. The content of.htaccess should be like:
RewriteEngine On
RewriteBase /
RewriteRule ^index.html$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.html [L]
Add the following before the last line
RewriteCond %{REQUEST_URI} !(\.png|\.jpg|\.gif|\.jpeg|\.bmp)$

Rewrite logic breaking?

I have the following rewrite logic in my vHost and everything seems to be working in regards to redirecting subdomains, but as soon as I add a path to the URI I'm getting an error in my apache_error.log.
Here is the rewrite logic:
RewriteEngine On
# Remove the www alias
RewriteCond %{HTTP_HOST} ^www\.13labs\.net$ [NC]
RewriteRule ^(.+)$ http://13labs.net$1 [R=301,L]
RewriteCond %{REQUEST_URI} ^.+$
RewriteCond %{REQUEST_FILENAME} !\.(gif|jpe?g|png|js|css|swf|php|ico|txt|pdf)$ [OR]
RewriteCond %{REQUEST_FILENAME} !-f [OR]
RewriteCond %{REQUEST_FILENAME} !-d [OR]
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^ - [L]
RewriteCond %{HTTP_HOST} !^www\.13labs\.net$ [NC]
RewriteCond %{HTTP_HOST} ^(www\.)?([^\.]+)\.13labs\.net$ [NC]
RewriteRule ^(.+)$ /index.php?subdomain=%2&kohana_uri=$1 [PT,L,QSA]
RewriteRule ^(.+)$ /index.php?kohana_uri=$1 [PT,L,QSA]
I am trying to hit admin.13labs.net/login, which should be rewriting to 13labs.net/index.php?subdomain=admin&kohana_uri=/login. However, in my apache_error.log I am receiving the following:
[Mon Aug 30 23:56:06 2010] [error] [client 74.63.151.37] File does not exist: /var/www/13labs.net/html/login
Any clues? I've been playing around with this for about an hour now and I'm stumped...
Regards,
Andrew
Your second rewrite rule (with its corresponding conditions) looks like it'll stop rewrites for any URL. It matches if the file doesn't have the right extension, OR isn't a file, OR isn't a directory, OR isn't a link. No resource can be a file and a dir and a link at the same time, so all URLs will match -- and they'll all get passed as is, since your [L] flag prevents any subsequent rewrites.
If your intention is to prevent rewrites for URLs that correspond to existing files, links, or directories, remove the !'s from your conditions and remove the [OR] from the condition that checks the file extension.

How can I use mod_rewrite to redirect to index.php except for assets?

I would like to have mod_rewrite go into effect for any filenames that don't exists except if that file name ends with js, css, gif, etc., so those would return regular 404s...
I tried this out:
RewriteCond %{REQUEST_FILENAME} \.(js|ico|gif|jpg|png|css|pdf)$ [OR]
RewriteCond %{REQUEST_FILENAME} favicon.ico$ [OR]
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^(.*)$ /index.php/$1 [L]
Which I found online somewhere, but it doesn't seem to work. My non existant JS files still get routed to my index.php file.
Try this rule:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule !.*\.(js|ico|gif|jpg|png|css|pdf)$ /index.php%{REQUEST_URI} [L]
Have you added the commands that go before the .htaccess file?
Like this line?
RewriteEngine On
You might want to use the [NC] flag on the first line if you can have not only .js files but also .Js or .JS or .jS files...

How do I write a custom mod_rewrite .htaccess file for CakePHP routing?

I've rewritten my web app using CakePHP, but now I need to have my old formatted urls redirect to my new url format. I can't seem to add my own custom mod rewrite rule. I've added it above the main cakephp rewrite rule, but I'm getting an infinite redirect loop. I just want http://mysite.com/index.php?action=showstream&nickname=user to redirect to http://mysite.com/user before the cakephp rewrite happens.
EDIT: Ok, so now when the condition is met it's redirecting but it's appending the original query string to the end. I'm assuming that's due to the QSA flag in CakePHP rewrite rules, but I was under the impression the "L" in my rule would stop that from executing...
RewriteEngine On
RewriteCond %{QUERY_STRING} ^action\=showstream&nickname\=(.*)$
RewriteRule ^.*$ http://mysite.com/%1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?url=$1 [QSA,L]
When you do a capture inside the RewriteCond line instead of the RewriteRule, you have to reference the capture with %N instead of $N. That is, your RewriteRule line should be:
RewriteRule ^index.php$ /%1 [R=301,L]
Try to test the request line (THE_REQUEST) to see what URI originally has been requested:
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /index\.php
RewriteCond %{QUERY_STRING} ^action=showstream&nickname=([^&]*)$
RewriteRule ^index\.php$ /%1? [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule .* index.php?url=$0 [QSA,L]
But maybe it would be easier to do this with PHP.

Resources