Why isn't this mod_rewrite re-directing? - mod-rewrite

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)

Related

URL Rewriting (Mod_Rewrite)

Hi I know absolutely nothing about mod_rewrite.
I'm asking how I could rewrite this URL as needed.
website.com?page=pagename&content=contents&action=download
as
website.com/pagename/contents/download
the param "action" will not always be set. Unsure if that makes a difference.
Try adding this to the htaccess in your document root:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/?$ /?page=$1&content=$2&action=$3 [L,QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^/]+)/?$ /?page=$1&content=$2 [L,QSA]

Rewrite ignoring files in a directory when it exists

I have been using successfully the following rewrite that takes www.site.com/123/abc to points it to www.site.com/index.php?id=123&page=abc
RewriteEngine On
RewriteRule ^([0-9]+)$ http://%{HTTP_HOST}/$1/ [R,L]
RewriteRule ^([0-9]+)/(.*)$ /index.php?id=$1&page=$2 [L]
Until now. When a directory that exists and matches the $1 www.site.com/123/ the files in the /123/ directory I no have access to. If I try changing the rewrite to the following everything stills works except the files.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([0-9]+)$ http://%{HTTP_HOST}/$1/ [R,L]
RewriteRule ^([0-9]+)/(.*)$ /index.php?id=$1&page=$2 [L]
What would one use to keep using mod rewrite and not ignore files in a directory when it exists.
RewriteConds only apply to the very next RewriteRule. Since you have two rules you need to repeat your conditions.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([0-9]+)$ http://%{HTTP_HOST}/$1/ [R,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([0-9]+)/(.*)$ /index.php?id=$1&page=$2 [L]
Or you could filter out files and directories at the top before continuing with your rewrite rules:
# Do nothing if URL points to existing file or directory.
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule (.*) $1 [L]
RewriteRule ^([0-9]+)$ http://%{HTTP_HOST}/$1/ [R,L]
RewriteRule ^([0-9]+)/(.*)$ /index.php?id=$1&page=$2 [L]

Mod Rewrite rules for a subdirectory

I have a drupal installation set up in a subdirectory of a domain (www.mydomain.com/cms), but I'm having trouble getting the queries to format correctly using mod_rewrite. I need url requests coming in as:
{domain}/cms/admin/content/node
to be interpreted as:
{domain}/cms/?q=admin/content/node
Here's what I have:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !=/favicon.ico
RewriteCond %{REQUEST_URI} ^/(cms/.*)$
RewriteRule ^(.*)$ cms/?q=$1 [L,QSA]
Any pointers on where I'm going wrong?
Where these rules you posted are located? If you create a .htaccess inside the "cms" directory with the following content, I believe it should work fine:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !=/favicon.ico
RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]

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 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