Strange behavior RewriteCond on Apache 2.2 - mod-rewrite

working .htaccess config:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteCond %{REQUEST_URI} ^thumbs/(.*)$
RewriteRule ^(.+)$ /index.php [L,QSA]
All 404 queries to /thumbs/ folder not must be catched by /index.php script. Why top .htaccess config work and bottom config not work?
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteCond %{REQUEST_URI} !^/thumbs/(.*)$
RewriteRule ^(.+)$ /index.php [L,QSA]
Apache 2.2.9, Debian

REQUEST_URI contains the requested URI path and does always start with a slash.
So the pattern ^thumbs/(.*)$ does never match as it’s missing the leading /. But the other condition, !^/thumbs/(.*)$, should match every request that’s URI path does not start with /thumbs/.

I think it's because the first slash in
RewriteCond %{REQUEST_URI} !^/thumbs/(.*)$

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]

Mod_Rewrite Exclude a url (URI Exception)

I am using this mod-rewrite rule to redirect the just the homepage of my site to the /it/ sub-directory on the same site.
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
RedirectMatch 301 ^/$ http://www.example.com/it/
However, I want to exempt this url from being redirected
http://www.example.com/?act=25
How can I write an exempt condition to do this?
Found a solution
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
RewriteCond %{QUERY_STRING} !^act=(.*)$ [NC]
RewriteRule ^$ http://www.example.com/it/
Oh, and RewriteCond does not work with RedirectMatch in case you didn't know (I didn't and wasted hrs because of it)

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]

combining force-www with clean urls

I have this mod-rewrite in my htacces which enables some clean urls;
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /
### LANGUAGE REDIRECT RULES start
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(en|nl)-?()?\/(.*\/?)$ index.php?language=$1&region=$2&symphony-page=$3&%{QUERY_STRING} [L]
### LANGUAGE REDIRECT RULES end
### FRONTEND REWRITE - Will ignore files and folders
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*\/?)$ ./index.php?symphony-page=$1&%{QUERY_STRING} [L]
</IfModule>
Now I would like to also force www, so I extended the code:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]
RewriteBase /
### LANGUAGE REDIRECT RULES start
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(en|nl)-?()?\/(.*\/?)$ index.php?language=$1&region=$2&symphony-page=$3&%{QUERY_STRING} [L]
### LANGUAGE REDIRECT RULES end
### FRONTEND REWRITE - Will ignore files and folders
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*\/?)$ ./index.php?symphony-page=$1&%{QUERY_STRING} [L]
</IfModule>
However this results in none of my pages being found.
FYI A language redirect adds country-code parameters to the url on load.
How do I get these rules to play along?
You're probably missing the query string:
RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [QSA,L,R=301]
You can also use the QSA flag in your other rules, e.g.:
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*\/?)$ ./index.php?symphony-page=$1 [L,QSA,B]
I also added B because you're using the backreference in a query string, so it needs to be escaped.
See the documentation.

Resources