Using backreference after RewriteCond in htaccess - .htaccess

I have:
RewriteCond %{HTTP_HOST} ^my.domain.com$ [NC]
RewriteRule ^(.*)$ index.php?q=search&keyword=$1
Input:
my.domain.com/foo_bar
I want:
index.php?q=search&keyword=foo_bar
But in fact:
index.php?q=search&keyword=index.php
I don't understand why. Please help me!

Your rewrite rule is actually rewriting twice, once for /foo_bar and second time for index.php as .* matches anything.
You just need to add 2 conditions to stop rewrite for files and directories:
# handle landing page
RewriteCond %{HTTP_HOST} ^my\.domain\.com$ [NC]
RewriteRule ^/?$ index.php?q=search [L,QSA]
# handle /foo_bar
RewriteCond %{HTTP_HOST} ^my\.domain\.com$ [NC]
# If the request is not for a valid directory
RewriteCond %{REQUEST_FILENAME} !-d
# If the request is not for a valid file
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+)$ index.php?q=search&keyword=$1 [L,QSA]

Related

Remove trailing slash with htaccess with existing rules

I wish to remove a trailing slash when one is given using htaccess. What would be the best way to do it that will work with my existing rules as below:
# make sure www. is always there
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}/$1 [R=301,L]
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
# if requested url does not exist pass it as path info to index.php
RewriteRule ^$ index.php?/ [QSA,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*) index.php?/$1 [QSA,L]
A sample URL would be something like:
https://www.example.com/this-test/
I of course want the ending slash removed.
I've tried this:
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)/$ /$1 [R,L]
But that does not work with the existing rules that are there. It ends up redirecting to the index.php pages due to the other rules.
Have it this way:
Options -MultiViews
DirectoryIndex index.php
RewriteEngine On
# add www and turn on https in same rule
RewriteCond %{HTTP_HOST} !^www\. [NC,OR]
RewriteCond %{HTTPS} !on
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^ https://www.%1%{REQUEST_URI} [R=301,L,NE]
# Unless directory, remove trailing slash
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} ^(.+)/+$
RewriteRule ^ %1 [R=301,NE,L]
# if requested url does not exist pass it as path info to index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php?/$0 [QSA,L]
Make sure to test it after completely clearing browser cache.

htaccess no-trailing-slash policy: non existing urls (404) should not redirect

If I open a non existing url on my site like www.domain.de/abcde/ (with trailing slash) the url redirects to www.domain.de/abcde (without trailing slash) and opens the 404 site after.
But the 404 site should come directly when the url does not exist (without redirecting for the non-trailing-slash policy).
What do I need to add to my htaccess? Thank you!
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} (.+)/$
RewriteRule ^ %1 [R=301,L]
------
starkeens suggestion does only work on a clean htaccess, but I got some more code. It does not work together with the other lines. All the code:
RewriteEngine on
RewriteBase /
# redirect urls without www. to url with www.
RewriteCond %{HTTP_HOST} ^domain\.de$
RewriteRule ^(.*)$ http://www.domain.de/$1 [L,R=301]
# hide the suffix of urls
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /(.+)\.html\ HTTP/
RewriteRule (.*).html$ /$1 [R=301,L]
# start redirect everything to the subfolder
RewriteCond %{HTTP_HOST} ^(www\.)?domain\.de [NC]
# except these folders:
RewriteCond %{REQUEST_URI} !^/downloads [NC]
RewriteCond %{REQUEST_URI} !^/cms [NC]
RewriteCond %{REQUEST_URI} !^/vorschau [NC]
RewriteCond %{REQUEST_URI} !^/domain/.*$
RewriteRule ^(.*)$ /subfolder/$1
# end redirect everything to the subfolder
# no-trailing-slash policy
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} (.+)/$
RewriteRule ^ %1 [R=301,L]
Any new suggestions? :)
TRY :
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ - [R=404,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} (.+)/$
RewriteRule ^ %1 [R=301,L]
Finally i got a solution that seems to work.. maybe anyone got the same issue and needs the code. Comments for optimization are welcome!
# trailing Slashes
# to enforce a no-trailing-slash policy with subfolder
# is the request for a non-existent file?
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# if so, skip the next RewriteRule
RewriteRule .? - [S=1]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} (.+)/$
RewriteRule ^ %1 [R=301,L]
Now only existing files get the redirect of my no-trailing-slash policy. Non existing files aren't redirected and occur a 404 page directly, as wished.

.htaccess redirect only redirects after the first link is clicked

I have an .htaccess file that is supposed to be redirecting to several different domains/subdirectories:
RewriteEngine on
#this works
RewriteCond %{HTTP_HOST} ^(www\.)?domainA\.ca$ [NC]
RewriteCond %{DOCUMENT_ROOT}/foundation%{REQUEST_URI}/ -d
RewriteRule [^/]$ %{REQUEST_URI}/ [R=301,L]
#this works
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{HTTP_HOST} ^(www\.)?domainB\.ca$ [NC]
RewriteRule ^(.*)$ /foundation/$1 [QSA,L]
#this sort of works
RewriteCond %{HTTP_HOST} .
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
#RewriteRule ^(.+)$ index.php?/$1 [QSA]
RewriteRule (.*) http://www.domainC.ca/index.php?/$1 [R=301,QSA]
They have several more domains set up as aliases on the account domainD, domainE and so on. If someone requests domainD then the browser's address bar will say on domainD = no rewrite, as soon as they click a link they will get forwarded to domainC/whatever-link/ which is correct, but what am I missing to get the inital domainD -> domainC rewrite to work?
You can try these rules:
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\.domainC\.ca$ [NC]
RewriteRule ^ http://www.domainC.ca%{REQUEST_URI} [R=301,L,NE]

strange htaccess query parameters

I have searched some on this site but couldn't find a right answer to my question.
I am trying to force a www redirect and forcing an endslash on each url.
I have the following lines in my htaccess:
# enable rewriting
RewriteEngine on
# if not a file or folder, use index.php
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^(.*)$ index.php?url=$1 [L]
# force www
RewriteCond %{HTTP_HOST} !^www
RewriteRule .? http://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
# force endslash
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule (.*) %{REQUEST_URI}/ [L,R=301]
My www redirect is working fine, it will go like:
http://example.com
to
http://www.example.com
But now the strange part is, my end slash line is adding the url parameter which I don't want.
So this will go like:
http://www.example.com/path/without/endlash
to
http://www.example.com/index.php/?url=path/without/endslash
Why is it that my defined url parameter is used in this case, and how can I prevent this.
Thanks in advance
EDIT:
thanks to icrew
Only added no-file/no-dir before the endlash entry because otherwise it would redirect my assets.
Final code:
RewriteCond %{HTTP_HOST} !^www
RewriteRule .? http://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{REQUEST_URI} !=/index.php
RewriteRule (.*) %{REQUEST_URI}/ [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?url=$1 [QSA,L]
This part of .htaccess
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^(.*)$ index.php?url=$1 [L]
is responsible for http://www.example.com/index.php/?url=path/without/endslash redirection. So basicly if you do not want url parameter in query string remove those three lines.
Edit: I figure out what you wanted. Bellow is correct code
# enable rewriting
RewriteEngine on
# force www
RewriteCond %{HTTP_HOST} !^www
RewriteRule .? http://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
# force endslash
RewriteCond %{REQUEST_URI} !(.*)/$
#next row prevent redirection if final rewriting is done
RewriteCond %{REQUEST_URI} !=/index.php
RewriteRule (.*) %{REQUEST_URI}/ [L,R=301]
# if not a file or folder, use index.php
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
#QSA because I suppose you dont want to discard the existing query string. Remove QSA if you want to discard
RewriteRule ^(.*)$ index.php?url=$1 [QSA,L]

.htaccess redirect from 1 url (no matter what file/folder) to another url

I have a website http://rochesterwaterskishow.com which they've recently changed their name so they want to update their url to http://skidox.com. I'm trying to redirect any page from rochesterwaterskishow.com to skidox.com/site/index.
I have this line of code which redirects http://rochesterwaterskishow.com to http://skidox.com, but if I go to something like http://rochesterwaterskishow.com/test, it doesn't redirect to http://skidox.com.
RewriteRule ^$ http://skidox.com/site/index [R=301,L]
How can I make it a catch all so anything rochesterwaterskishow.com/* gets redirected to skidox.com/site/index?
UPDATE: Full .htaccess file
RewriteEngine on
RewriteCond $1 !^(index\.php|resources|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L,QSA]
RewriteRule ^$ http://skidox.com/site/index [R=301,L]
That's because the search pattern ^$ will only match a URI path of "/". You need to pick up the request in a match variable, for example:
RewriteCond %{HTTP_HOST} rochesterwaterskishow
RewriteRule ^.* http://skidox.com/site/index/$0 [R=301,L]
I am assuming that you are using SEO optimised-style URIs for the new site. If you want to simply redirect everything to the index page without any context, then you still need a pattern that matches:
RewriteCond %{HTTP_HOST} rochesterwaterskishow
RewriteRule ^ http://skidox.com/site/index [R=301,L]
Update following post of full htaccess
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} rochesterwaterskishow
RewriteRule ^.* http://skidox.com/$0 [R=301,L]
RewriteCond $0 ^(index\.php$|robots\.txt$|resources)
RewriteRule ^.* - [S=1]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L,QSA]
RewriteRule ^$ http://skidox.com/site/index/$1 [R=301,L]

Resources