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].
Related
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.
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.
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]
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]
in typo3 htacces finish all redirection by RewriteRule .* index.php [QSA],
but I need create PromoCode foldier with php aplication and own index.php work under adres www.domainname.com/PromoCode. So i need to add exception into htacces, but not sure how...
RewriteRule ^rss\/([^\.]+).xml$ fileadmin/rss/$1.xml [QSA,L]
#RewriteRule ^fileadmin\/(.*)$ fileadmin/$1 [QSA,L]
# SPECIAL
RewriteRule ^ua-ru/links807835.php fileadmin/links807835.php [QSA,L]
# PAGING
RewriteRule ^(.*)(?!order\/.*)\/order\/([^-]+)-([^\/]+)(.*)$ $1$4?pg_order=$2&pg_by=$3 [QSA,L]
RewriteRule ^(.*)(?!order\/.*)\/order\/([^\/]+)(.*)$ $1$3?pg_order=$2 [QSA,L]
RewriteRule ^(.*)(?!page\/.*)\/page\/([^\/]+)(.*)$ $1$3?pg_page=$2 [QSA,L]
RewriteRule ^(.*)(?!size\/.*)\/size\/([^\/]+)(.*)$ $1$3?pg_size=$2 [QSA,L]
# FAQ
RewriteRule ^(.*)(?!faq\/.*)\/faq\/([^\/]+)(.*)$ $1$3?faq=$2 [QSA,L]
# GALLERY
RewriteRule ^(.*)(?!gallery\/.*)\/gallery\/([^\/]+)(.*)$ $1$3?gallery=$2 [QSA,L]
# ARTICLE
RewriteRule ^(.*)(?!article\/.*)\/article\/([^\/]+)(.*)$ $1$3?article=$2 [QSA,L]
# PRODUCT
RewriteRule ^(.*)(?!product\/.*)\/product\/([^\/]+)(.*)$ $1$3?product=$2 [QSA,L]
# EVENT
RewriteRule ^(.*)(?!event\/.*)\/event\/([^\/]+)(.*)$ $1$3?event=$2 [QSA,L]
# MATERIAL
RewriteRule ^(.*)(?!material\/.*)\/material\/([^\/]+)(.*)$ $1$3?material=$2 [QSA,L]
# CATEGORY
RewriteRule ^(.*)(?!category\/.*)\/category([^\/]+)([^\/]+)(.*)$ $1$3?category=$2 [QSA,L]
# LANGUAGE
RewriteRule ^([^\/]*)\/$ $1?id=$1 [QSA]
RewriteRule ^([^\/]*)$ $1?id=$1 [QSA]
#RewriteRule ^([^\/]*)[\/]{0,1}(.*)$ $2?lang=$1 [QSA]
RewriteRule ^([^\/]*)[\/]{0,1}(.*)$ ?lang=$1 [QSA]
# PAGE
RewriteRule ^[\/]{0,1}([^\/]+)[\/]([^\/]+)[\/]([^\/]+)[\/]{0,1}(.*)$ ?id=$3&pid1=$2&pid2=$1 [QSA]
RewriteRule ^[\/]{0,1}([^\/]+)[\/]([^\/]+)[\/]{0,1}(.*)$ ?id=$2&pid1=$1 [QSA]
RewriteRule ^[\/]{0,1}([^\/]+)[\/]{0,1}(.*)$ ?id=$1 [QSA]
# FINISH
RewriteCond %{REQUEST_URI} !^/PromoCode/
RewriteRule .* index.php [QSA]
Before the RewriteRule .* index.php line, add this condition:
RewriteCond %{REQUEST_URI} !^/promocode/
make the /promocode/ folder whatever the foldername is that you want to make an exception for.