Apache 2.2 how to check if HTTP_HOST SERVER_ADDR are not equal? - mod-rewrite

I have tried the below mentioned option but its not working. The condition passes when both are equal AND when both the values are not equal.
RewriteEngine on
RewriteCond %{HTTP_HOST} !^%{SERVER_ADDR}
RewriteRule ^(.*) http://hostIsDifferent [R=301,L]

Related

Regard a couple of OR-RewriteCond directives if another RewriteCond evals

I have the following configuration,
RewriteCond %{HTTPS} !^on$ [nocase,ornext]
RewriteCond %{HTTP_HOST} !^www\. [nocase,ornext]
RewriteCond %{HTTP_HOST} !\.com$ [nocase]
RewriteRule ^(.*)$ https://www.acme.com/$1 [redirect=301,last]
the purpose is to redirect to a canonical URL if the request is either not HTTPS, nor begins with www. or ends with .com.
For being seamlessly compatible with developer engines, I want to exclude all these directives if %{HTTP_HOST} includes, for example, dev.internal or so. In this case the RewriteRule should be skipped immediately. Since the three ORs are evaluated with the higher precedence than an (implicit) AND, I wonder how and where to place my dev.internal exception...
Thanks for any advice!
//edit: hmm... if OR has the higher precendence, shouldn't
RewriteCond %{HTTP_HOST} !internal\. [nocase]
RewriteCond %{HTTP:X-Forwarded-Proto} !^https$ [nocase,ornext]
RewriteCond %{HTTP_HOST} !^www\. [nocase,ornext]
RewriteCond %{HTTP_HOST} !\.com$ [nocase]
RewriteRule ^(.*)$ https://www.acme.com/$1 [redirect=301,last]
work then?
If my understanding of ornext is correct then yes, your way should work (can someone else confirm it?).
Here's another way, if you don't want to rely on that:
RewriteCond %{HTTP_HOST} internal\. [nocase] # If it's an internal host...
RewriteRule .* - [skip=1] # ... skip the next rule (and leave the URL unchanged)
RewriteCond %{HTTP:X-Forwarded-Proto} !^https$ [nocase,ornext]
RewriteCond %{HTTP_HOST} !^www\. [nocase,ornext]
RewriteCond %{HTTP_HOST} !\.com$ [nocase]
RewriteRule ^(.*)$ https://www.acme.com/$1 [redirect=301,last]

Rewriting URLs with one query string to another URL with a different query string

I'm trying to accomplish some redirects as follows:
#Basic Rule Set
RewriteRule ^/path$ http://newhost.com/Page?pagename=form1000 [R=301]
RewriteRule ^/path/index.html$ http://newhost.com/Page?pagename=form1000 [R=301]
which work fine.
However, redirects with query strings in source and destination return me to the above destination url. For e.g.,
# Advanced Rule Set
RewriteRule ^/path/index.html?var=foo$ http://newhost.com/Page?pagename=form1000?id=hello [R=301]
RewriteRule ^/path/index.html?var=bar$ http://newhost.com/Page?pagename=form1000?id=byebye [R=301]
RewriteRule ^/path/index.html?var=xyz$ http://newhost.com/Page?pagename=form1000?id=world [R=301]
all redirect to http://newhost.com/Page?pagename=form1000.
I've tried a RewriteCond %{QUERY_STRING} ^var=(.*)$ [NC] and RewriteCond %{REQUEST_URI} ^/path/index.html [NC] preceding the above three rules, and still I'm redirected to http://newhost.com/Page?pagename=form1000. I've swaped the order of the Basic and Advanced rule sets, and all redirect to http://newhost.com/Page?pagename=form1000.
Any ideas on how I might get these rule sets to work? CentOS 6.x, Apache 2.2.
You may try this instead:
# Advanced Rule Set
RewriteCond %{QUERY_STRING} var=foo [NC]
RewriteRule ^path/index.html/? http://newhost.com/Page?pagename=form1000?id=hello [R=301,L,NC]
RewriteCond %{QUERY_STRING} var=bar [NC]
RewriteRule ^path/index.html/? http://newhost.com/Page?pagename=form1000?id=byebye [R=301,L,NC]
RewriteCond %{QUERY_STRING} var=xyz [NC]
RewriteRule ^path/index.html/? http://newhost.com/Page?pagename=form1000?id=world [R=301,L,NC]

Apache mod_rewrite friendly URLs with corresponding 301 redirects

The Problem:
Been spinning my wheels and reading up on this one for awhile and looking for some help now. I'm looking to take a group of non-friendly URLs (there are actually more "groups" but this should me for an example):
domainname.com/?section=zebras
domainname.com/?section=monkeys&id=555
and turn them into friendly URLs, as well as do a 301 on the old versions, so that any old bookmarks (and search engines) will still resolve them. The new format I'm looking for would be:
domainname.com/zebras/
domainname.com/monkeys/555
I'm fully intending to write separate RewriteCond/RewriteRule combinations for each of those scenarios, so I don't necessarily need a super-rule that catches all my scenarios. Oh and this is all in .htaccess.
My Progress:
I was originally getting into a redirect loop because I was just doing two RewriteRules back to back - one for the friendly URL and one for the 301 redirect. Came across my favorite way (so far) around the redirect loop which works (for my scenario #1 at least):
RewriteCond %{ENV:REDIRECT_STATUS} !200
RewriteCond %{QUERY_STRING} ^section=zebras$ [NC]
RewriteRule ^.*$ http://www.domainname.com/zebras/? [R=301,NC,L]
RewriteRule ^zebras/$ /index\.php?section=zebras [NC,L]
However, I'd like to have something that works for more than just "zebras" (for instance, I'd like it to work for "lions" as well), so I'm trying to make that more generic. What I am trying now looks like this:
RewriteCond %{ENV:REDIRECT_STATUS} !200
RewriteCond %{QUERY_STRING} ^section=([a-z]+)$ [NC]
RewriteRule ^section=([a-z]+)$ http://www.domainname.com/$1/? [R=301,NC,L]
RewriteRule ^([a-z]+)/$ /index\.php?section=$1 [NC,L]
However, this doesn't work. I think I have something "not quite right", I just can't tell what it is - there's something I'm missing or formatting incorrectly somewhere. Sorry in advance for the lengthy description, just wanted to be clear.
Do this:
RewriteEngine on
RewriteBase /
RewriteCond %{ENV:REDIRECT_STATUS} 200
RewriteRule ^ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(\.\w+|/)$
RewriteRule (.*) /$1/ [R,L]
RewriteCond %{QUERY_STRING} ^section=([a-z]+)$ [NC]
RewriteRule ^ /%1/? [R=301,NC,L]
RewriteRule ^([a-z]+)/$ /index\.php?section=$1 [NC,L]
RewriteCond %{QUERY_STRING} ^section=([a-z]+)&id=(\d+)$ [NC]
RewriteRule ^ /%1/%2/? [R=302,NC,L]
RewriteRule ^([a-z]+)/(\d+)/$ /index\.php?section=$1&id=$2 [NC,L]
Description
Prevents looping:
RewriteCond %{ENV:REDIRECT_STATUS} 200
RewriteRule ^ - [L]
Prevents trailing slash problem:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(\.\w+|/)$
RewriteRule (.*) /$1/ [R,L]
Handles rewrites with only section=([a-z]+) in them:
RewriteCond %{QUERY_STRING} ^section=([a-z]+)$ [NC]
RewriteRule ^ /%1/? [R=302,NC,L]
RewriteRule ^([a-z]+)/$ /index\.php?section=$1 [NC,L]
Handles rewrites with only section=([a-z]+)&id=(\d+) in them:
RewriteCond %{QUERY_STRING} ^section=([a-z]+)&id=(\d+)$ [NC]
RewriteRule ^ /%1/%2/? [R=302,NC,L]
RewriteRule ^([a-z]+)/(\d+)/$ /index\.php?section=$1&id=$2 [NC,L]
mistake in your rules:
section=([a-z]+) is not available in the URI part. So, RewriteRule ^section=([a-z]+)$ never matched.

Mod rewrite issue with https, strip www from url

we have a url for eg https://www.egdomain.com/ and the ssl is valid for egdomain.com.
so how can i redirect all the requests from https://www.egdomain.com/ to https://egdomain.com/
the site also has normal http requests which works fine.
I tried the htaccess below but still nothing
RewriteCond %{HTTP_HOST} ^www\.(.*) [NC]
RewriteCond %{HTTP_PORT} =443
RewriteRule (.*) https://%1/$1 [L,R=301]
RewriteCond %{HTTP_HOST} ^www\.(.*) [NC]
RewriteRule (.*) http://%1/$1 [L,R=301]
Any help will e much appreciated
You can combine these two rules to one:
RewriteCond %{HTTPS}s ^on(s)|
RewriteCond http%1://%{HTTP_HOST}%{REQUEST_URI} ^(https?://)www\.(.+) [NC]
RewriteRule ^ %1%2 [L,R=301]
But maybe this is a little too confusing.
The manual says (emphasis mine):
RewriteCond backreferences: These are backreferences of the form %N (1 <= N <= 9), which provide access to the grouped parts (again, in parentheses) of the pattern, from the last matched RewriteCond in the current set of conditions.
So:
RewriteCond %{HTTP_PORT} =443
RewriteCond %{HTTP_HOST} ^www\.(.*) [NC]
RewriteRule (.*) https://%1/$1 [L,R=301]
Note that if your certificate is only valid for the variant without www., visitors will still get a security warning when accessing the www. variant.

Cannot able use Multiple rule in mod_rewrie

I need used use it
RewriteEngine On
RewriteCond %{SERVER_PORT} !^9090$
RewriteRule ^vendor/([0-9]+).html$
productcategory.iface?Operation=category&catid=$1 [L,R]
RewriteRule ^vendor/([0-9]+)([0-9]+).html$
productcategory.iface?Operation=product&prodid=$2
RewriteRule ^vendor/([0-9]+)([0-9]+)([0-9]+).html$
productcategory.iface?Operation=vendet&prodid=$2&venid=$3
But I Used this rule i found Error on 500 that is misconfiguration error
Line breaks inside the directives are not allowed. So try this:
RewriteEngine On
RewriteCond %{SERVER_PORT} !^9090$
RewriteRule ^vendor/([0-9]+)\.html$ productcategory.iface?Operation=category&catid=$1 [L,R]
RewriteRule ^vendor/([0-9]+)\([0-9]+)\.html$ productcategory.iface?Operation=product&prodid=$2
RewriteRule ^vendor/([0-9]+)\([0-9]+)\([0-9]+)\.html$ productcategory.iface?Operation=vendet&prodid=$2&venid=$3

Resources