Apache2, HTTP_REFERER usage - mod-rewrite

I'd like to allow access to sitea.com, only if the request came from siteb.com, if not, it is redirected to site3.com. Is HTTP_REFERER used for this? Is it limited to links in siteb.com to sitea.com? Should I use something else? Here is a quick example that came to my mind (its rushed and for sure not the best approach):
RewriteCond %{HTTP_REFERER} https://site1\.domain1.com/ [NC]
RewriteRule ^ - [S=3]
RewriteCond %{HTTP_REFERER} https://site2\.domain2.com/ [NC]
RewriteRule ^ - [S=1]
RewriteRule ^(.*)$ https://site1\.domain1.com/ [L,R=301]
Thanks!

Related

RewriteCond %{REQUEST_URI} not working

I am trying to redirect all requests coming in to the web server as http://portal.company.com/legacy to http://portal.company.com/wps/portal/public/legacy/legacyportlet with the following rule, but it is not working as expected.
RewriteEngine on
RewriteCond %{HTTP_HOST} ^portal\.company\.com$ [NC]
RewriteCond %{REQUEST_URI} ^/legacy$ [NC]
RewriteRule ^(.*)$ /wps/portal/public/legacy/legacyportlet$1 [NC,L,PT]
I have also tried
RewriteCond %{HTTP_HOST} ^portal\.company\.com$ [NC]
RewriteRule ^/legacy /wps/portal/public/legacy/legacyportlet [NC,L,PT]
Any help would be greatly appreciated!
Thanks
It doesn't look like your source or target URLs change in any way, so possibly you're better off using Apache's basic Redirect directive which just redirects one URL to another.
Use this rule:
RewriteCond %{HTTP_HOST} ^portal\.company\.com$ [NC]
RewriteRule ^legacy/?$ /wps/portal/public/legacy/legacyportlet [NC,L]
Remember that in .htaccess RewriteRule doesn't match leading slash of URI.

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.

Nested subdomain URL rewrite

I have a sight that is of the following form:
nested_subdomain1.nested_subdomain2.domain.com
It might be something like test.users.domain.com and I would like to be able to rewrite this URL to something like test.users.domain2.com.
So far, my luck has not proven well and I have not been able to successfully implement a working solution from examples found online. I have tried some things like the following:
RewriteCond %{HTTP_HOST} ^domain\.com
RewriteRule (.*) http://domain2.com/$1 [R=301,L]
Or this one...
RewriteCond %{HTTP_HOST} !^fully\.qualified\.domain\.name$ [NC]
RewriteCond %{HTTP_HOST} !^$
RewriteRule ^/(.*) http://fully.qualified.domain.name/$1 [L,R]
I am not sure what I am doing wrong and feel like I am missing something really obvious.
Try this
#match anything1.anything2.domain.com
RewriteCond %{HTTP_HOST} ^([^.]+\.[^.]+)\.domain\.com$ [NC]
#redirect to anything1.anything2.domain2.com
RewriteRule ^ http://%1.domain2.com%{REQUEST_URI} [R=301,L]
RewriteCond %{HTTP_HOST} domain\.com$
RewriteRule (*.).mydomain.com mydomain.com/$1
This will trasnfer xx.yy.mydomain.com to mydomain.com/xx.yy
To replace with slashes, try
RewriteCond %{HTTP_HOST} domain\.com$
RewriteRule (*.\.).mydomain.com mydomain.com/$1/$2/$3
To transfer to another domain ,try
RewriteCond %{HTTP_HOST} domain\.com$
RewriteRule (*.).mydomain.com $1.mydomain.com [R=301,L]
This will transfer the subdomains upto a level of three. Frankly, you will have to analyze the host in your index.php to determine which subdomain is caled, so might as well use the first one

Refactoring a mod_rewrite depending if query string exisits

RewriteCond %{QUERY_STRING} lang_opt\=E
RewriteRule /lc/courses.asp english-resource [R=301,NC,L]
RewriteCond %{QUERY_STRING} lang_opt\=F
RewriteRule /lc/courses.asp french-resource [R=301,NC,L]
RewriteRule /lc/courses.asp english-resource [R=301,NC,L]
A link comes in 3 flavors
/lc/courses.asp
/lc/courses.asp?lang=E
/lc/courses.asp?lang=F
Is there a more elegant way to deal with these 3 variations of a link. I have quite a few of them and 5 line of mod_rewite per seems excessive.
Thanks
Since English is the default, you only need to look for French and redirect everything else to English:
RewriteCond %{QUERY_STRING} lang_opt=F
RewriteRule ^/lc/courses\.asp$ french-resource [R=301,NC,L]
RewriteRule ^/lc/courses\.asp$ english-resource [R=301,NC,L]

RewriteCond to exclude a directory and it's sub-directories

I've been trying to get a wikipedia style language thing to work. So that the url will be en.example.com for English, fr.example.com for French, etc... This is working fine however I would like the admin area to always default to base language, i.e. not set the LANGUAGE environment variable. I've tried adding RewriteCond %{REQUEST_URI} !^admin [NC] but it seems to have no effect.
My mod_rewrite code is as follows:
# Handle languages
# Picks up the language code from the browser accept-language parameter
RewriteCond %{HTTP:Accept-Language} ^([a-z]{2}).*$ [NC]
RewriteCond %{HTTP_HOST} !^[a-z]{2}\.[a-z]{2,}\. [NC]
RewriteRule ^(.*)$ http://%1.%{HTTP_HOST}/$1 [R=301]
RewriteCond %{REQUEST_URI} !^admin [NC]
RewriteCond %{HTTP_HOST} ^([a-z]{2})\.[a-z]{2,}\. [NC]
RewriteRule ^(.*)$ - [ENV=LANGUAGE:%1,QSA]
# Redirecting all requests to one script
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([\+a-zA-Z0-9,%\(\)\_\ -/]+)$ /index.php [NC,L,QSA]
Thanks for any help, I'm sure it's something really stupid that is wrong, as usual.
Your problem is that REQUEST_URI doesn't start with admin, its going to have a slash in front... it might start with /admin if you have no rewrite base... so you can change it to !^/admin or just !admin or !admin/ if all your code is in foo.com/admin/*
REQUEST_URI is going to be the entire GET like "/foo/bar.html"

Resources