htaccess rewrite condition redirect to root domain - apache

I am just simply trying to redirect several pages to the root domain and unsure why its failing, nothing happens.
RewriteEngine On
RewriteCond %{QUERY_STRING} /category/uncategorized/
RewriteCond %{QUERY_STRING} /2014/12/
RewriteCond %{QUERY_STRING} attachment_id=19
RewriteCond %{QUERY_STRING} attachment_id=18
RewriteCond %{QUERY_STRING} attachment_id=5
RewriteCond %{QUERY_STRING} attachment_id=20
RewriteCond %{QUERY_STRING} attachment_id=72
RewriteCond %{QUERY_STRING} attachment_id=6
RewriteCond %{QUERY_STRING} attachment_id=10
RewriteCond %{QUERY_STRING} attachment_id=15
RewriteCond %{QUERY_STRING} attachment_id=14
RewriteCond %{QUERY_STRING} attachment_id=16
RewriteCond %{QUERY_STRING} attachment_id=17
RewriteCond %{QUERY_STRING} attachment_id=99
RewriteCond %{QUERY_STRING} attachment_id=44
RewriteRule http://domain.com? [L,R=301]
The WP rewrites are in this same .htaccess but no change if I remove them.

Something like the below should work. Make sure it's in the .htaccess in your DocumentRoot
RewriteEngine On
RewriteRule category/uncategorized/? http://domain.com? [L,R=301]
RewriteRule 2014/12/? http://domain.com? [L,R=301]
RewriteCond %{QUERY_STRING} attachment_id=([56]|1[4-9]|20|44|72|99)
RewriteRule ^ http://domain.com? [L,R=301]

Edit: #arco444's answer will work for you (and is indeed a better answer), but it is essential that you try and understand exactly what is happening if you want to use it.
Option 1
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/category/uncategorized/ [OR]
RewriteCond %{REQUEST_URI} ^/2014/12/ [OR]
RewriteCond %{QUERY_STRING} attachment_id=19 [OR]
RewriteCond %{QUERY_STRING} attachment_id=18 [OR]
RewriteCond %{QUERY_STRING} attachment_id=5 [OR]
RewriteCond %{QUERY_STRING} attachment_id=20 [OR]
RewriteCond %{QUERY_STRING} attachment_id=72 [OR]
RewriteCond %{QUERY_STRING} attachment_id=6 [OR]
RewriteCond %{QUERY_STRING} attachment_id=10 [OR]
RewriteCond %{QUERY_STRING} attachment_id=15 [OR]
RewriteCond %{QUERY_STRING} attachment_id=14 [OR]
RewriteCond %{QUERY_STRING} attachment_id=16 [OR]
RewriteCond %{QUERY_STRING} attachment_id=17 [OR]
RewriteCond %{QUERY_STRING} attachment_id=99 [OR]
RewriteCond %{QUERY_STRING} attachment_id=44
RewriteRule ^ http://domain.com? [L,R=301]
The main thing here is the [OR] flag. In your original code, you were basically saying that all conditions had to be met. Remember that the last condition should not have the [OR].
Secondly, you were using QUERY_STRING to check for category/..., but that has nothing to do with the query string, which appear after the ?. So, I have change that to %{REQUEST_URI}.
Also, your RewriteRule did not have a URI to match against. I have added a ^ before the domain name to say that it should match everything (that is, provided the conditions have been met).
Option 2
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/category/uncategorized/ [OR]
RewriteCond %{REQUEST_URI} ^/2014/12/ [OR]
RewriteCond %{QUERY_STRING} attachment_id=(5|6|10|14|15|16|17|18|19|20|44|72|99)
RewriteRule ^ http://domain.com? [L,R=301]
Here, I have simplified the query string check. We now use one condition to check for different attachment IDs.
You could probably simplify this even further, but the purpose of my answer is to let you know where you went wrong, and where things can be simplified a little.

Related

I am having issues adding multiple url parameters to the same line in .htaccess

Currently my .htaccess looks like this
RewriteCond %{QUERY_STRING} ^&
RewriteRule ^$ - [R=404]
RewriteCond %{QUERY_STRING} ^age
RewriteRule ^$ - [R=404]
RewriteCond %{QUERY_STRING} ^gender
RewriteRule ^$ - [R=404]
RewriteCond %{QUERY_STRING} ^languages
RewriteRule ^$ - [R=404]
RewriteCond %{QUERY_STRING} ^sites
RewriteRule ^$ - [R=404]
RewriteCond %{QUERY_STRING} ^sortOrder
RewriteRule ^$ - [R=404]
RewriteCond %{QUERY_STRING} ^status
RewriteRule ^$ - [R=404]
RewriteCond %{QUERY_STRING} ^tags
RewriteRule ^$ - [R=404]
At the moment this works well, if I visit a URL with one of the parameters it will give me a 404 page, I want to know if there is a better way to write this.
Is it possible to combine all these into one line?
I have tried writing it like this
RewriteCond %{QUERY_STRING} ^&
RewriteCond %{QUERY_STRING} ^age
RewriteCond %{QUERY_STRING} ^gender
RewriteCond %{QUERY_STRING} ^languages
RewriteCond %{QUERY_STRING} ^sites
RewriteCond %{QUERY_STRING} ^sortOrder
RewriteCond %{QUERY_STRING} ^status
RewriteCond %{QUERY_STRING} ^tags
RewriteRule ^$ - [R=404]
But that didn't work as it would only work for the top query and not the rest of them
I have tried writing it like this
RewriteCond %{QUERY_STRING} ^&
RewriteCond %{QUERY_STRING} ^age
RewriteCond %{QUERY_STRING} ^gender
RewriteCond %{QUERY_STRING} ^languages
RewriteCond %{QUERY_STRING} ^sites
RewriteCond %{QUERY_STRING} ^sortOrder
RewriteCond %{QUERY_STRING} ^status
RewriteCond %{QUERY_STRING} ^tags
RewriteRule ^$ - [R=404]
RewriteCond directives (conditions) are implicitly AND'd, so the above will never be successful (ie. no 404 occurs) since the query string can not match all those strings at the same time.
You need to use the OR flag on all but the last condition. For example:
RewriteCond %{QUERY_STRING} ^& [OR]
RewriteCond %{QUERY_STRING} ^age [OR]
RewriteCond %{QUERY_STRING} ^gender [OR]
RewriteCond %{QUERY_STRING} ^languages [OR]
RewriteCond %{QUERY_STRING} ^sites [OR]
RewriteCond %{QUERY_STRING} ^sortOrder [OR]
RewriteCond %{QUERY_STRING} ^status [OR]
RewriteCond %{QUERY_STRING} ^tags
RewriteRule ^$ - [R=404]
However, this can be further reduced using regex alternation. For example, the above is the same as the following using just one condition:
RewriteCond %{QUERY_STRING} ^(&|age|gender|languages|sites|sortOrder|status|tags)
RewriteRule ^$ - [R=404]

Apache2, mod_rewrite - use variable from prev condition in next condition

I would like to check if file exist in subdirectory.
And I mean subdirectory that "is taken" from "subdomain".
Eg. url: "test.domain.com/file.txt" file location is "/test/file.txt"
I have wildacrd and i can't hardcode "test" directory. How can i do it?
I tried add %{DOCUMENT_ROOT}%1/$1 but this dosen't work.
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{HTTP_HOST} !^www\.
RewriteCond %{HTTP_HOST} ^([^.]+)\.domain.com
RewriteCond %{DOCUMENT_ROOT}%1/$1 -f [OR]
RewriteCond %{DOCUMENT_ROOT}%1/$1 -d
RewriteRule ^ /%1%{REQUEST_URI} [L]
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{HTTP_HOST} !^www\.
RewriteCond %{HTTP_HOST} ^([^.]+)\.domain.com
RewriteRule ^images/(.*)$ /%1/image.php?test=$1 [L]
With your shown samples, please try following. Please make sure to clear your browser cache before testing your URLs.
Options +FollowSymLinks -MultiViews
RewriteEngine ON
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} ^([^.]+)\.domain.com [NC]
RewriteCond %{DOCUMENT_ROOT}/%1/$1 -f [OR]
RewriteCond %{DOCUMENT_ROOT}/%1/$1 -d
RewriteRule ^(.*)/?$ %1/$1 [L]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} ^([^.]+)\.domain.com [NC]
RewriteRule ^images/(.*)/?$ %1/image.php?test=$1 [QSA,NC,L]
Fixes in OP's attempts: You need not to use condition RewriteCond %{ENV:REDIRECT_STATUS} ^$ in 2nd Rules sets, even first one may not need it but for safer side you can have it. Then your sequence of accessing path from %{DOCUMENT_ROOT} was not correct which was corrected here.

htaccess combine/reduce rewrite conditions into one (or at least fewer)

I have a course booking component that is unfortunately providing three different URLs to the same page, depending on whether you go in via a menu link, breadcrumb or link within the component.
As such, I've been adding a new rewrite condition to an htaccess file, per course, as follows:
RewriteCond %{REQUEST_URI} ^/citb/smsts/book-now$ [NC,OR]
RewriteCond %{REQUEST_URI} ^/citb/smsts/book-now/$ [NC,OR]
RewriteCond %{REQUEST_URI} ^/citb/smsts/book-now/1$ [NC]
RewriteRule .* /citb/smsts/book-now/1-smsts [R=301,NC,L]
RewriteCond %{REQUEST_URI} ^/citb/smsts-refresher/book-now$ [NC,OR]
RewriteCond %{REQUEST_URI} ^/citb/smsts-refresher/book-now/$ [NC,OR]
RewriteCond %{REQUEST_URI} ^/citb/smsts-refresher/book-now/2$ [NC]
RewriteRule .* /citb/smsts-refresher/book-now/2-smsts-refresher [R=301,NC,L]
RewriteCond %{REQUEST_URI} ^/citb/sssts/book-now$ [NC,OR]
RewriteCond %{REQUEST_URI} ^/citb/sssts/book-now/$ [NC,OR]
RewriteCond %{REQUEST_URI} ^/citb/sssts/book-now/3$ [NC]
RewriteRule .* /citb/sssts/book-now/3-sssts [R=301,NC,L]
RewriteCond %{REQUEST_URI} ^/citb/sssts-refresher/book-now$ [NC,OR]
RewriteCond %{REQUEST_URI} ^/citb/sssts-refresher/book-now/$ [NC,OR]
RewriteCond %{REQUEST_URI} ^/citb/sssts-refresher/book-now/4$ [NC]
RewriteRule .* /citb/sssts-refresher/book-now/4-sssts-refresher [R=301,NC,L]
RewriteCond %{REQUEST_URI} ^/citb/health-and-safety-awareness/book-now$ [NC,OR]
RewriteCond %{REQUEST_URI} ^/citb/health-and-safety-awareness/book-now/$ [NC,OR]
RewriteCond %{REQUEST_URI} ^/citb/health-and-safety-awareness/book-now/7$ [NC]
RewriteRule .* /citb/health-and-safety-awareness/book-now/7-health-and-safety-awareness [R=301,NC,L]
RewriteCond %{REQUEST_URI} ^/iosh/managing-safely/book-now$ [NC,OR]
RewriteCond %{REQUEST_URI} ^/iosh/managing-safely/book-now/$ [NC,OR]
RewriteCond %{REQUEST_URI} ^/iosh/managing-safely/book-now/8$ [NC]
RewriteRule .* /iosh/managing-safely/book-now/8-iosh-managing-safely [R=301,NC,L]
RewriteCond %{REQUEST_URI} ^/iosh/working-safely/book-now$ [NC,OR]
RewriteCond %{REQUEST_URI} ^/iosh/working-safely/book-now/$ [NC,OR]
RewriteCond %{REQUEST_URI} ^/iosh/working-safely/book-now/9$ [NC]
RewriteRule .* /iosh/working-safely/book-now/9-iosh-working-safely [R=301,NC,L]
RewriteCond %{REQUEST_URI} ^/first-aid/3-day-first-aid-at-work/book-now$ [NC,OR]
RewriteCond %{REQUEST_URI} ^/first-aid/3-day-first-aid-at-work/book-now/$ [NC,OR]
RewriteCond %{REQUEST_URI} ^/first-aid/3-day-first-aid-at-work/book-now/5$ [NC]
RewriteRule .* /first-aid/3-day-first-aid-at-work/book-now/5-3-day-first-aid-at-work [R=301,NC,L]
RewriteCond %{REQUEST_URI} ^/first-aid/1-day-first-aid-at-work/book-now$ [NC,OR]
RewriteCond %{REQUEST_URI} ^/first-aid/1-day-first-aid-at-work/book-now/$ [NC,OR]
RewriteCond %{REQUEST_URI} ^/first-aid/1-day-first-aid-at-work/book-now/6$ [NC]
RewriteRule .* /first-aid/1-day-first-aid-at-work/book-now/6-1-day-first-aid-at-work [R=301,NC,L]
This is obviously not a desirable approach to keep repeating every time there's a new course to be added (and yes I know the best solution would actually be to hack the component around so that it's not doing this) so I was wondering if there was a way of making this more generic, either with the one rewrite condition or maybe one per category or however it could be best generalised?
Each desired/destination URL will follow the following format:
/[category]/[course]/book-now/[id]-[alias]
Where, from the first example above:
category = citb
course = smsts
id = 1
alias = smsts
Many thanks in advance!
Actually, i don't think (or i don't know how) you could reduce all your redirections (you can't actually guess the missing number in your 2 first conditions).
But you could actually handle all your 3rd conditions with :
RewriteEngine On
RewriteRule ^([^/]*)/([^/]*)/book-now/([0-9]+)$ /$1/$2/book-now/$3-$2 [R=301,NC,L]
And combine your 2 first conditions by using (/?) at the end.
That would make :
RewriteEngine On
RewriteRule ^([^/]*)/([^/]*)/book-now/([0-9]+)$ /$1/$2/book-now/$3-$2 [R=301,NC,L]
RewriteCond %{REQUEST_URI} ^/citb/smsts/book-now(/?)$ [NC]
RewriteRule .* /citb/smsts/book-now/1-smsts [R=301,NC,L]
...
PS : You wrote :
Each desired/destination URL will follow the following format:
/[category]/[course]/book-now/[id]-[alias]
Where, from the first example above:
category = citb
course = smsts
id = 1
alias = smsts
But actually, for your /iosh/working-safely/book-now/9 url, your format is :
alias = [category]-[course]
Since it becomes
/iosh/working-safely/book-now/9-iosh-working-safely
Not sure if it's a mistake in your htaccess, seems important to notice.
Hope it helps !

Apache Redirect - same domain different REQUEST_URI

I need multiple RewriteCond for the same domain, but different REQUEST_URIs
I need to redirect to a different domain depending on the REQUEST_URI.
I am trying this with no luck:
RewriteCond %{HTTP_HOST} ^(www.)?example.com$ 
RewriteCond %{REQUEST_URI} !^/myuir/ [OR]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteRule (.*)$ / [R=301,L]
RewriteCond %{HTTP_HOST} ^(www.)?example.com$ 
RewriteCond %{REQUEST_URI} !^/myuniqueuri/ [OR]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteRule (.*)$ http://example2.com/$! [R=301,L]
RewriteCond %{HTTP_HOST} ^(www.)?example.com$ 
RewriteCond %{REQUEST_URI} !^/myotheruniqueuri/ [OR]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteRule (.*)$ http://example3.com/$! [R=301,L]
I thought the [OR] on the request uri condition would work, but it did not. I have also tried without the [OR] and it did not work!
For example:
if the url is:
http://example.com/myuniqueuri
needs to redirect to:
http://example2.com
if the url is:
http://example.com/myotheruniqueuri
needs to redirect to:
http://example3.com
where example2 and example3 can be anything but are not the same.
Any help is greatly appreciated
Thanks
I think the problem might be in your syntax. The reference is $1 not $!. Also the ! is being used incorrectly. This basically means not. Like if not this directory then etc. Well it appears you want to match the directory and then redirect based on that. Try this code and see if it works for you.
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/myuir
RewriteRule ^(.*)$ / [R=301,L]
RewriteCond %{REQUEST_URI} ^/myuniqueuri
RewriteRule ^(.*)$ http://example2.com/ [R=301,L]
RewriteCond %{REQUEST_URI} ^/myotheruniqueuri
RewriteRule ^(.*)$ http://example3.com/ [R=301,L]

Redirect from few url's (both www and non-www) to original one

How to shorten this expression? i want instead of using 2 lines just check whatever domain is something different from mysite.com e.g. mysite.org or www.mysite.org with or without www and redirect.
Options +FollowSymlinks
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.mysite\.com$ [OR]
RewriteCond %{HTTP_HOST} ^mysite\.org$ [OR]
RewriteCond %{HTTP_HOST} ^www\.mysite\.org$ [OR]
RewriteCond %{HTTP_HOST} ^mysite\.net$ [OR]
RewriteCond %{HTTP_HOST} ^www\.mysite\.net$
RewriteRule ^(.*)$ http://mysite.com/$1 [R=301,L]
Also is it faster to use [OR] or do like this
RewriteCond %{HTTP_HOST} ^www\.mysite\.com$
RewriteRule ^(.*)$ http://mysite.com/$1 [R=301,L]
RewriteCond %{HTTP_HOST} ^mysite\.org$
RewriteRule ^(.*)$ http://mysite.com/$1 [R=301,L]
RewriteCond %{HTTP_HOST} ^www\.mysite\.org$
RewriteRule ^(.*)$ http://mysite.com/$1 [R=301,L]
RewriteCond %{HTTP_HOST} ^mysite\.net$
RewriteRule ^(.*)$ http://mysite.com/$1 [R=301,L]
P.S. why this works as case insensetive even when i dont use [NC]?
You may want to check Apache documentation on canonical hostnames, your rules can be expressed as a negation
RewriteEngine On
RewriteCond %{HTTP_HOST} !^mysite\.com$ [NC]
RewriteCond %{HTTP_HOST} !^$
RewriteRule ^(.*)$ http://mysite.com/$1 [L,R=301]
And for the rules matching without [NC] , it probably depends on what the browser sends to the server.