Rewrite Rule to exclude images - .htaccess

I've got the following htaccess code and I need it to only work if it is not images. Right now its making all the images seem as if they don't exist on the pages on the site.
# If there are caps, set HASCAPS to true and skip next rule
RewriteRule [A-Z] - [E=HASCAPS:TRUE,S=1]
# Skip this entire section if no uppercase letters in requested URL
RewriteRule ![A-Z] - [S=28]
# Replace single occurance of CAP with cap, then process next Rule.
RewriteRule ^([^A]*)A(.*)$ $1a$2
RewriteRule ^([^B]*)B(.*)$ $1b$2
RewriteRule ^([^C]*)C(.*)$ $1c$2
RewriteRule ^([^D]*)D(.*)$ $1d$2
RewriteRule ^([^E]*)E(.*)$ $1e$2
RewriteRule ^([^F]*)F(.*)$ $1f$2
RewriteRule ^([^G]*)G(.*)$ $1g$2
RewriteRule ^([^H]*)H(.*)$ $1h$2
RewriteRule ^([^I]*)I(.*)$ $1i$2
RewriteRule ^([^J]*)J(.*)$ $1j$2
RewriteRule ^([^K]*)K(.*)$ $1k$2
RewriteRule ^([^L]*)L(.*)$ $1l$2
RewriteRule ^([^M]*)M(.*)$ $1m$2
RewriteRule ^([^N]*)N(.*)$ $1n$2
RewriteRule ^([^O]*)O(.*)$ $1o$2
RewriteRule ^([^P]*)P(.*)$ $1p$2
RewriteRule ^([^Q]*)Q(.*)$ $1q$2
RewriteRule ^([^R]*)R(.*)$ $1r$2
RewriteRule ^([^S]*)S(.*)$ $1s$2
RewriteRule ^([^T]*)T(.*)$ $1t$2
RewriteRule ^([^U]*)U(.*)$ $1u$2
RewriteRule ^([^V]*)V(.*)$ $1v$2
RewriteRule ^([^W]*)W(.*)$ $1w$2
RewriteRule ^([^X]*)X(.*)$ $1x$2
RewriteRule ^([^Y]*)Y(.*)$ $1y$2
RewriteRule ^([^Z]*)Z(.*)$ $1z$2
# If there are any uppercase letters, restart at very first RewriteRule in file.
RewriteRule [A-Z] - [N]
RewriteCond %{ENV:HASCAPS} TRUE
RewriteRule ^/?(.*) /$1 [R=301,L]
I've already tried adding RewriteCond %{REQUEST_URI} !\.(gif|jpe?g|png)$ [NC] but either it didn't work or I placed it in the wrong place.

Place this rule at top of your .htaccess to skip all existing files and directories:
# skip all files and directories from rewrite rules below
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]

Related

Load website in a subdirectory using .htaccess

I need to make a fully working backup of the actual site in a subdirectory, but I don't understand why it doesn't work, I mean the website is not showing correctly.
Here is the .htaccess code of the root directory. I need to make a backup in a subdirectory "/old/".
RewriteEngine On
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} -f [OR]
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} -d
RewriteRule ^ - [L]
RewriteCond %{HTTP_HOST} ^([a-z.]+)?example\.com$ [NC]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule .? http://www.%example.com%{REQUEST_URI} [R=301,L]
RewriteRule ^([^/]*)/css/(.*) /css/$2 [L]
RewriteRule ^([^/]*)/js/(.*) /js/$2 [L]
RewriteRule ^([^/]*)/fonts/(.*) /fonts/$2 [L]
RewriteRule ^([^/]*)/images/(.*) /images/$2 [L]
RewriteRule ^([^/]*)/templates/(.*) /templates/$2 [L]
RewriteRule ^([^/]*)/$ /index.php?lang=$1 [L]
RewriteRule ^([^/]*)/([^/]*)$ /index.php?lang=$1 [L]
RewriteRule ^([^/]*)/([^/]*)/([^/]*)$ /index.php?lang=$1 [L]
RewriteRule ^([^/]*)/([^/]*)/([^/]*)/([^/]*)$ /index.php?lang=$1 [L]
Thank you for your help!
RewriteRule ^([^/]*)/css/(.*) /css/$2 [L]
RewriteRule ^([^/]*)/js/(.*) /js/$2 [L]
RewriteRule ^([^/]*)/fonts/(.*) /fonts/$2 [L]
RewriteRule ^([^/]*)/images/(.*) /images/$2 [L]
RewriteRule ^([^/]*)/templates/(.*) /templates/$2 [L]
RewriteRule ^([^/]*)/$ /index.php?lang=$1 [L]
RewriteRule ^([^/]*)/([^/]*)$ /index.php?lang=$1 [L]
RewriteRule ^([^/]*)/([^/]*)/([^/]*)$ /index.php?lang=$1 [L]
RewriteRule ^([^/]*)/([^/]*)/([^/]*)/([^/]*)$ /index.php?lang=$1 [L]
You just need to remove the slash prefix from all the substitution strings in the above directives. You can then use the same .htaccess file in both the root and in the backup subdirectory.
For example:
RewriteRule ^([^/]*)/css/(.*) css/$2 [L]
RewriteRule ^([^/]*)/js/(.*) js/$2 [L]
RewriteRule ^([^/]*)/fonts/(.*) fonts/$2 [L]
RewriteRule ^([^/]*)/images/(.*) images/$2 [L]
RewriteRule ^([^/]*)/templates/(.*) templates/$2 [L]
RewriteRule ^([^/]*)/$ index.php?lang=$1 [L]
RewriteRule ^([^/]*)/([^/]*)$ index.php?lang=$1 [L]
RewriteRule ^([^/]*)/([^/]*)/([^/]*)$ index.php?lang=$1 [L]
RewriteRule ^([^/]*)/([^/]*)/([^/]*)/([^/]*)$ index.php?lang=$1 [L]
Removing the slash prefix makes the substitution relative to the directory that contains the .htaccess file.
These 9 directives can be reduced/simplified into 2 if you wish:
RewriteRule ^[^/]+/((?:css|js|fonts|images|templates)/.*) $1 [L]
RewriteRule ^([^/]+)(?:/[^/]*){1,3}$ index.php?lang=$1 [L]
UPDATE: I've fixed the regex in the 2nd "simplified" rule since when there is only one path segment the trailing slash is mandatory (previously it would not have matched if the trailing slash was included). For two and three path segments the trailing slash is effectively optional and for four path segments, there should be no trailing slash. Although whether that was really the intention of the original directives is another matter. The 2nd "simplified" rule now matches that behaviour. (Previously, it did not permit a trailing slash with any number of path segments.)
Aside:
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} -f [OR]
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} -d
RewriteRule ^ - [L]
RewriteCond %{HTTP_HOST} ^([a-z.]+)?example\.com$ [NC]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule .? http://www.%example.com%{REQUEST_URI} [R=301,L]
You should reverse the order of these two rules. Otherwise, the first rule will prevent the canonical redirect (that prefixes the www subdomain) from happening on direct file requests.

.htaccess code to make urls lowercase and to add trailing slash when needed adds an extra trailing slash on occasion. Any ideas how to fix it?

I have a simple website with two pages that have the following URLs:
http://www.gulfcoastgetaway.org/donate/
http://www.gulfcoastgetaway.org/questions/
I have an .htaccess file that I've pieced together to make sure that a trailing slash is added (/donate => /donate/) and also to make things lowercase (/Donate => /donate/).
But a url that has a capital letter and has a trailing slash (/DOnate/) gets rewritten with extra trailing slashes (one extra one per character that gets transformed to lowercase). So /Donate/ => /donate// and /DONate/ => /donate////
I don't really know how to fix this, as my familiarity with .htaccess is limited.
RewriteEngine on
RewriteBase /
# If there are caps, set HASCAPS to true and skip next rule
RewriteRule [A-Z] - [E=HASCAPS:TRUE,S=1]
# Skip this entire section if no uppercase letters in requested URL
RewriteRule ![A-Z] - [S=28]
# Replace single occurance of CAP with cap, then process next Rule.
RewriteRule ^([^A]*)A(.*)$ $1a$2
RewriteRule ^([^B]*)B(.*)$ $1b$2
RewriteRule ^([^C]*)C(.*)$ $1c$2
RewriteRule ^([^D]*)D(.*)$ $1d$2
RewriteRule ^([^E]*)E(.*)$ $1e$2
RewriteRule ^([^F]*)F(.*)$ $1f$2
RewriteRule ^([^G]*)G(.*)$ $1g$2
RewriteRule ^([^H]*)H(.*)$ $1h$2
RewriteRule ^([^I]*)I(.*)$ $1i$2
RewriteRule ^([^J]*)J(.*)$ $1j$2
RewriteRule ^([^K]*)K(.*)$ $1k$2
RewriteRule ^([^L]*)L(.*)$ $1l$2
RewriteRule ^([^M]*)M(.*)$ $1m$2
RewriteRule ^([^N]*)N(.*)$ $1n$2
RewriteRule ^([^O]*)O(.*)$ $1o$2
RewriteRule ^([^P]*)P(.*)$ $1p$2
RewriteRule ^([^Q]*)Q(.*)$ $1q$2
RewriteRule ^([^R]*)R(.*)$ $1r$2
RewriteRule ^([^S]*)S(.*)$ $1s$2
RewriteRule ^([^T]*)T(.*)$ $1t$2
RewriteRule ^([^U]*)U(.*)$ $1u$2
RewriteRule ^([^V]*)V(.*)$ $1v$2
RewriteRule ^([^W]*)W(.*)$ $1w$2
RewriteRule ^([^X]*)X(.*)$ $1x$2
RewriteRule ^([^Y]*)Y(.*)$ $1y$2
RewriteRule ^([^Z]*)Z(.*)$ $1z$2
# If there are any uppercase letters, restart at very first RewriteRule in file.
RewriteRule [A-Z] - [N]
RewriteCond %{ENV:HASCAPS} TRUE
RewriteRule ^/?(.*) /$1 [R=301,L]
# add trailing slash
RewriteCond %{REQUEST_URI} !(/$|\.)
RewriteRule (.*) %{REQUEST_URI}/ [R=301,L]
As I said, I need it to not add those extra trailing slashes.

Redirecting uppercase urls to lowercase urls using htaccess.

I have tried below code, its redirecting but effecting my css and js files. So I want to ignore all the folders. How it can be done? I tried using RewriteCond %{REQUEST_FILENAME} !-d but doesn't work.
RewriteEngine On
RewriteBase /
# If there are caps, set HASCAPS to true and skip next rule
RewriteRule [A-Z] - [E=HASCAPS:TRUE,S=1]
# Skip this entire section if no uppercase letters in requested URL
RewriteRule ![A-Z] - [S=28]
# Replace single occurance of CAP with cap, then process next Rule.
RewriteRule ^([^A]*)A(.*)$ $1a$2
RewriteRule ^([^B]*)B(.*)$ $1b$2
RewriteRule ^([^C]*)C(.*)$ $1c$2
RewriteRule ^([^D]*)D(.*)$ $1d$2
RewriteRule ^([^E]*)E(.*)$ $1e$2
RewriteRule ^([^F]*)F(.*)$ $1f$2
RewriteRule ^([^G]*)G(.*)$ $1g$2
RewriteRule ^([^H]*)H(.*)$ $1h$2
RewriteRule ^([^I]*)I(.*)$ $1i$2
RewriteRule ^([^J]*)J(.*)$ $1j$2
RewriteRule ^([^K]*)K(.*)$ $1k$2
RewriteRule ^([^L]*)L(.*)$ $1l$2
RewriteRule ^([^M]*)M(.*)$ $1m$2
RewriteRule ^([^N]*)N(.*)$ $1n$2
RewriteRule ^([^O]*)O(.*)$ $1o$2
RewriteRule ^([^P]*)P(.*)$ $1p$2
RewriteRule ^([^Q]*)Q(.*)$ $1q$2
RewriteRule ^([^R]*)R(.*)$ $1r$2
RewriteRule ^([^S]*)S(.*)$ $1s$2
RewriteRule ^([^T]*)T(.*)$ $1t$2
RewriteRule ^([^U]*)U(.*)$ $1u$2
RewriteRule ^([^V]*)V(.*)$ $1v$2
RewriteRule ^([^W]*)W(.*)$ $1w$2
RewriteRule ^([^X]*)X(.*)$ $1x$2
RewriteRule ^([^Y]*)Y(.*)$ $1y$2
RewriteRule ^([^Z]*)Z(.*)$ $1z$2
# If there are any uppercase letters, restart at very first RewriteRule in file.
RewriteRule [A-Z] - [N]
RewriteCond %{ENV:HASCAPS} TRUE
RewriteRule ^/?(.*) /$1 [R=301,L]

htaccess rewrite " " with "-"

Here's my htaccess code:
RewriteEngine On
RewriteRule ^contact$ contact.php [QSA,L]
RewriteRule ^error$ error.php [QSA,L]
RewriteRule ^([^/.]+)/?$ game.php?title=$1 [L,QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule . /error [L,R=301]
How can make it to work for example:
localhost/Test1%20Test2 or localhost/Test1 Test2
to work with
localhost/test1-test2
Have this rule s your very first rule:
RewriteRule "^([^ ]*) +([^ ]* .*)$" /$1-$2 [L]
RewriteRule "^([^ ]*) ([^ ]*)$" /$1-$2 [L,R=301]

.htaccess URL to lowercase

How to alter following snippet of .htaccess file to achieve:
Now if it rewrites, it takes user from-to:
domain.com/TEST - domain.com/test
domain.com/subdir/TEST - domain.com/test
Needed behaeviour is:
domain.com/TEST - domain.com/test
domain.com/subdir/TEST - domain.com/subdir/test
Unwanted behaeviour occurs when webpage's index is at 'subdir' directory, instead directly under domain.com, as shown in first example above.
# URL to lowercase
# If there are caps, set HASCAPS to true and skip next rule
RewriteRule [A-Z] - [E=HASCAPS:TRUE,S=1]
# Skip this entire section if no uppercase letters in requested URL
RewriteRule ![A-Z] - [S=28]
# Replace single occurance of CAP with cap, then process next Rule.
RewriteRule ^([^A]*)A(.*)$ $1a$2
RewriteRule ^([^B]*)B(.*)$ $1b$2
RewriteRule ^([^C]*)C(.*)$ $1c$2
RewriteRule ^([^D]*)D(.*)$ $1d$2
RewriteRule ^([^E]*)E(.*)$ $1e$2
RewriteRule ^([^F]*)F(.*)$ $1f$2
RewriteRule ^([^G]*)G(.*)$ $1g$2
RewriteRule ^([^H]*)H(.*)$ $1h$2
RewriteRule ^([^I]*)I(.*)$ $1i$2
RewriteRule ^([^J]*)J(.*)$ $1j$2
RewriteRule ^([^K]*)K(.*)$ $1k$2
RewriteRule ^([^L]*)L(.*)$ $1l$2
RewriteRule ^([^M]*)M(.*)$ $1m$2
RewriteRule ^([^N]*)N(.*)$ $1n$2
RewriteRule ^([^O]*)O(.*)$ $1o$2
RewriteRule ^([^P]*)P(.*)$ $1p$2
RewriteRule ^([^Q]*)Q(.*)$ $1q$2
RewriteRule ^([^R]*)R(.*)$ $1r$2
RewriteRule ^([^S]*)S(.*)$ $1s$2
RewriteRule ^([^T]*)T(.*)$ $1t$2
RewriteRule ^([^U]*)U(.*)$ $1u$2
RewriteRule ^([^V]*)V(.*)$ $1v$2
RewriteRule ^([^W]*)W(.*)$ $1w$2
RewriteRule ^([^X]*)X(.*)$ $1x$2
RewriteRule ^([^Y]*)Y(.*)$ $1y$2
RewriteRule ^([^Z]*)Z(.*)$ $1z$2
# If there are any uppercase letters, restart at very first RewriteRule in file.
RewriteRule [A-Z] - [N]
RewriteCond %{ENV:HASCAPS} TRUE
RewriteRule ^(.*) /$1 [R=301,L]
Please, remove the .htaccess present inside /subdir because then your rule /$ would remove subdir from the input URL since that leading / makes it an absolute URL. From the root .htaccess, the rule would leave the URL unchanged (except changing the case).
Alternatively, if you have any sub-directory specific rules and want to keep your /subdir/.htaccess just change it's rule to RewriteRule ^(.*)$ $1 [R=301,L].

Resources