Pattern matching in htaccess rewrite condition - apache

I have a rewrite condition in an .htaccess file which is used to 'not' force specific URLs to use a HTTPS connection but it's matching erroneously on other URLs at present, here's the rewrite rule...
RewriteCond %{HTTPS} off
RewriteCond !/go/ [NC]
RewriteCond %{HTTP_HOST} ^app\.ihasco\.co\.uk [NC]
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
And here's an example of the URL I want to match on in the 2nd line:
http://app.ihasco.co.uk/training/MTMwfG5pbmVmb3Vy/go/ST/bmF0aGFuQG5pbmVmb3VyLmNvLnVr
However line 2 seems to be matching on URLs like the following also:
http://app.ihasco.co.uk/client/login
and is subsequently failing to force them to HTTPS. What am I missing? :?

You need to tell the RewriteCond what to match !/go/ against. Change the second line to:
RewriteCond %{REQUEST_URI} !/go/ [NC]

Related

301 Redirect in htaccess for URLs having Parameter P

I need to set 301 redirect in htaccess for URLs having parameter P. One example URL is
http://www.price4india.co.in/vivo-x20-plus-ud-price-in-india-scanner-feature-real.html?p=1028
to
http://www.price4india.co.in/vivo-x20-plus-ud-price-in-india-scanner-feature-real.html
After redirect everything after .html shall get removed and the value after P=...... can be any numerical value. So far I have tried below query but it is not working. Any suggestion please...
RewriteCond %{QUERY_STRING} ^p(&|$) [NC]
RewriteRule ^ %{REQUEST_URI}? [R=301,L]
With your shown samples, please try following .htaccess rules file. Make sure to keep your .html file and .htaccess files in root path only.
Make sure to clear your browser cache before testing your URLs.
RewriteEngine ON
RewriteCond %{THE_REQUEST} \s/(.*\.html)\?p=\d+\s [NC]
RewriteRule ^ /%1? [R=301,L]
NOTE: In case you have further more rules in your .htaccess rules, which includes internal rewrite of html files then you could keep these rules above those.
RewriteCond %{QUERY_STRING} ^p(&|$) [NC]
RewriteRule ^ %{REQUEST_URI}? [R=301,L]
This is almost correct, except the regex ^p(&|$) is incorrect. This matches p&<anything> or p exactly. Whereas you need to match p=<anything> (eg. ^p=) or p=<number> (eg. ^p=\d+). This is of course assuming the p URL parameter always occurs at the start of the URL-path (as in your example).
For example:
RewriteCond %{QUERY_STRING} ^p= [NC]
RewriteRule ^ %{REQUEST_URI}? [R=301,L]

what is this htaccess rule actually doing

I have this condition and rule and want to know what actually is going on here
RewriteCond %{REQUEST_URI} ^/sitepages/newversion/
RewriteCond %{QUERY_STRING} ^(.*[?|&])page=dynamicpage
RewriteRule ^sitepages/newversion/(.*)$ /pages/oldversion/$1 [L]
Also very interested in a detail explaination of this line
RewriteRule ^sitepages/newversion/(.*)$ /pages/oldversion/$1 [L]
thanks
RewriteCond %{REQUEST_URI} ^/sitepages/newversion/
If the request URL starts with "/sitepages/newversion/",
RewriteCond %{QUERY_STRING} ^(.*[?|&])page=dynamicpage
and the query string contains "page=dynamicpage",
RewriteRule ^sitepages/newversion/(.*)$ /pages/oldversion/$1 [L]
then take the part of the URL after "sitepages/newversion/" and redirect the request to "/pages/oldversion/(the rest of the url)".
For example:
http://domain.com/sitepages/newversion/someleaf?page=dynamicpage
will get redirected to
http://domain.com/pages/oldversion/someleaf?page=dynamicpage
First of all your rule can be rewritten better as this:
RewriteCond %{QUERY_STRING} (?:^|&)page=dynamicpage(?:&|$) [NC]
RewriteRule ^sitepages/newversion/(.*)$ /pages/oldversion/$1 [L,NC]
Now for explanation part.
RewriteCond is matching a URI with query parameter page=dynamicpage
RewriteRule is matching a URI with pattern sitepages/newversion/(.*)$ which will match /sitepages/newversion/abc123 or /sitepages/newversion/foobar
(.*) is capturing group to populate $1 with the value abc123 in my first example.
In target we use /pages/oldversion/$1 that will become /pages/oldversion/abc123 for same example.
NC flag is for no case comparison.
L flag is for Last that makes mod_rewrite run the rewrite loop again.
Reference: Apache mod_rewrite Introduction
Apache mod_rewrite Technical Details

Rewrite condition to particular URL and redirect

I have a redirect rule that redirects a url of pattern /abcd/* to http://myweb.com/abcd/*.
I want to apply this redirect except /abcd/index.html.
I have tried this.
RewriteCond %{THE_REQUEST} !^/abcd/index
RewriteCond %{REQUEST_URI} !^/abcd/index [NC]
RewriteRule ^/abcd/(.+)$ http://myweb.com/abcd/$1 [NC,R=301,L]
I am not sure if it is correct.
Please suggest me the correct way of doing this.
Try this rule without leading slash in RewriteRule:
RewriteCond %{REQUEST_URI} !^/abcd/index [NC]
RewriteRule ^abcd/(.+)$ http://myweb.com/abcd/$1 [NC,R=301,L]
Difference is ^abcd/(.+)$ instead of ^/abcd/(.+)$
Your 2 conditions were redundant so I reduced it to one.
.htaccess is per directory directive and Apache strips the current directory path (thus leading slash) from RewriteRule URI pattern.

Restrict some url pattern in a apache redirect

I am writing a redirect to match any url of the patter /message/* here.
RewriteRule ^/message/(.+)$ http://abet.in/message/$1 [NC,R=301,L]
Now I want to modify it by not allowing some string pattern in url.
RewriteCond to check /message/index.html in the url.
1. Check if the request url contains /message/index.html.
2. If the condition is not met then do a redirect.
I tried the following methods. But I am not sure whether they are correct or not.
RewriteCond %{THE_REQUEST} !^/message/index
RewriteCond %{REQUEST_URI} !^/message/index [NC]
Could some one tell how to do this.
%{THE_REQUEST} contains a string that looks like this for a regular page request:
GET /message/index.html HTTP/1.1
And %{REQUEST_URI} looks like this:
/message/index.html
So your 2nd option is almost correct. You don't need a / at the start of the Pattern for RewriteRules.
Additionally, these two rules will prevent all requests that start /message/index from being redirected):
RewriteCond %{REQUEST_URI} !^/message/index [NC]
RewriteRule ^message/(.+)$ http://abet.in/message/$1 [NC,R=301,L]
If you only want to prevent /message/index.html and not /message/index.php or /message/index-of-something-else then do:
RewriteCond %{REQUEST_URI} !^/message/index\.html [NC]
RewriteRule ^message/(.+)$ http://abet.in/message/$1 [NC,R=301,L]

htaccess - rewritecond issue

I have an htacess file for apache that isn't working properly.
I have a default cactch-all that grabs all requests however it's still catching the matched URL above it (ajax/*). I thought that condition to match urls not containing ajax would stop it but it isn't.
RewriteRule ^ajax/(.*)$ process_lite.php [QSA,L]
RewriteRule ^resources/(.*)$ resources/$1 [L]
RewriteCond %{REQUEST_URI} !\.(css|jpg|js|gif|png)$
RewriteCond %{REQUEST_URI} !^ajax
RewriteRule ^(.*)$ process.php [QSA,L]
Can someone help me out?
It's probably because the first rule matches /ajax/ requests and rewrites it to /process_lite.php, thus the RewriteCond %{REQUEST_URI} !^ajax doesn't match since the URI is now process_lite.php. The regular expression you use won't match anything anyways because REQUEST_URI variable will start with a leading slash. You can try changing the condition to:
RewriteCond %{REQUEST_URI} !^/process_lite.php
Additionally, the "resources" rule doesn't seem to do anything except end rewriting, you could change it to this if that's the goal:
RewriteRule ^resources/ - [L]