Why subdomain rewrite does not work? - mod-rewrite

I want my subdomain to point directly to a folder.
I have found the following mod_rewrite script to setup this behavior.
Unfortunately it does not work.
When I navigate to fish.example.com, the browser displays 404 error with the following message.
"The requested URL / was not found on this server."
Do you know why?
How can I make it work?
# Internally rewrite <subdomain>.example.com/<URLpath> to example.com/subs/<subdomain/<URLpath>
RewriteEngine on
RewriteCond $1 !^fish/
RewriteCond %{HTTP_HOST} !^www\.example\.com
RewriteCond %{HTTP_HOST} ^([^.]+)\.example\.com
RewriteRule (.*) /fish/%1/$1 [L]
UPDATE
I have changed the script to the following, but in this case the browser redirects to example.com instead of example.com/fish
Do you know why?
RewriteCond $1 !^fish/
RewriteCond %{HTTP_HOST} !^www\.example\.com
RewriteCond %{HTTP_HOST} ^([^.]+)\.example\.com
RewriteRule ^fish/(.*) /fish/%1/$1 [L]

Try this:
RewriteEngine On
Rewrite Base / # Make sure it starts from the domain
RewriteCond %{HTTP_HOST} ^fish.example.com [NC] # Catch your subdomain
RewriteRule (.*)$ fish/$1 [L] # Reroute to folder.

Update:
I see... try this:
RewriteCond $1 !^fish/
RewriteCond %{HTTP_HOST} !^www\.example\.com
RewriteCond %{HTTP_HOST} ^([^.]+)\.example\.com
RewriteRule (.*) http://example.com/%1/$1 [R=301,L]
Basically a 301 redirect... %1 matches the subdomain in the previous RewriteCond, and $1 matches the original url

Related

Redirect subdomain, but only if site root requested

I can't seem to find an answer to this specific question:
How can I forward http://play.domain.com -- but NOT http://play.domain.com/xxx
To:
http://work.domain.com
Where "xxx" is any string.
In other words, if the user requests the site root, redirect, otherwise stay on the chosen URL.
I've tried
RewriteEngine On
RewriteCond %{HTTP_HOST} !work.domain.com [NC]
RewriteRule ^(.*)$ http://work.domain.com.com/$1 [R=301,L]
But it's not doing anything :-/
Enable mod_rewrite and .htaccess through httpd.conf and then put this code in your .htaccess under DOCUMENT_ROOT directory:
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^play\.domain\.com$ [NC]
RewriteRule ^/?$ http://work.domain.com/ [R=301,L]
Try:
RewriteEngine On
RewriteCond %{HTTP_HOST} !work.domain.com [NC]
RewriteRule ^$ http://work.domain.com.com/ [R=301,L]
to only redirect the blank request.

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.

Trouble redirecting subdomains to a folder

I have got this working:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^([^\.]+)\.domain\.co\.uk
RewriteRule ^(.*) http://domain.co.uk/%1 [L]
So it will redirect test.domain.co.uk to test.domain.co.uk/test
However, I would like it so that it changes the document root rather than the user seeing the redirect. I have read a few articles on here and tried a few things. But I am not sure how to debug any errors, it just doesn't work. The one other that I have tried:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} !^/-
RewriteCond %{HTTP_HOST} ^([^\./]+)
RewriteCond %{DOCUMENT_ROOT}/-%1 -d
RewriteRule ^(.*)$ -%1/$1 [L]
But this doesn't seem to work at all.
Any help would be appreciated.
Ian
Try something like this in your document root:
RewriteEngine On
# check if subdomain folder has the existing file, if so, serve it
RewriteCond %{HTTP_HOST} ^([^\.]+)\.domain\.co\.uk
RewriteCond %{DOCUMENT_ROOT}/%1%{REQUEST_URI} -f
RewriteRule ^(.*)$ /%1/$1 [L]
# check if subdomain folder has the existing directory WITH A TRAILING SLASH, if so serve it
RewriteCond %{HTTP_HOST} ^([^\.]+)\.domain\.co\.uk
RewriteCond %{DOCUMENT_ROOT}/%1%{REQUEST_URI} -d
RewriteRule ^(.*?)/$ /%1/$1/ [L]
# check if subdomain filder has the existing directory but missing trailing slash, redirect with it
RewriteCond %{REQUEST_URI} !/$
RewriteCond %{HTTP_HOST} ^([^\.]+)\.domain\.co\.uk
RewriteCond %{DOCUMENT_ROOT}/%1%{REQUEST_URI} -d
RewriteRule ^(.*)$ /$1/ [R=301,L]
This method does 2 things for you, if it's a 404, it won't try to internally rewrite, so if you go to: http://sub.domain.co.uk/this/path/does/not/exist/blah, you won't get a 404 message saying: /sub/this/path/does/not/exist/blah doesn't exist, just /this/path/does/not/exist/blah because the URI wasn't rewritten, and thus not exposing your underlying directory structure.
The second thing is that you probably have DirectorySlash turned on. Which means if you access a directory like: http://sub.domain.co.uk/this/path, missing the trailing slash, mod_dir will want to redirect and add that slash, but it'll probably do it wrong because the URI has been rewritten and will redirect you to: http://sub.domain.co.uk/sub/this/path/. We you pre-emptively add the trailing slash with the correct URI hiding the sub.

Rewrite redirecting first to cached content

I tried many combinations but none works. I trying to write rewrite rule which:
Assume URL of http://example.com/project/big_bunny
When user visit site rewrite first try to read file /cache/project/big_bunny.html but only if there is no any get or post data
If there is no cache rewrite process standard router rule: index.php?_rt=$1 [L,QSA]
Any suggestions?
Try this:
# Check if using POST method
RewriteCond %{THE_REQUEST} !^POST
# Check if already rewrote to cache
RewriteCond %{REQUEST_URI} !^/cache
# Check if cache exists
RewriteCond /cache%{REQUEST_URI}.html -f
# Check if contains any GET query string
RewriteCond %{QUERY_STRING} !.+
# Do the rewrite
RewriteRule ^/(.*) /cache/$1.html [L]
# Check if already rewrote to cache
RewriteCond %{REQUEST_URI} !^/cache
RewriteRule ^/(.*) index.php?_rt=$1 [L,QSA]
Try this:
# Check the cache, and rewrite if file exists
RewriteCond %{THE_REQUEST} !^POST [NC]
RewriteCond %{QUERY_STRING} ^$
RewriteCond %{DOCUMENT_ROOT}/cache%{REQUEST_URI}.html -f
RewriteRule ^ /cache%{REQUEST_URI}.html [L]
# If URI doesn't start with /cache/, it wasn't cached so rewrite to index.php
RewriteCond %{REQUEST_URI} !^/cache/
RewriteRule ^(.*)$ index.php?_rt=$1 [L,QSA]

301 redirect non-www to www not always working

I've read through a ton of posts and pages trying to figure this out. I have it mostly working. I have a .htaccess file setup and I'm trying to redirect (301) any page in my site from non-www to www version.
RewriteEngine On
RewriteCond %{HTTP_HOST} ^example.com [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]
It seems to work from the base url. However, it doesn't redirect for sub pages.
This works:
example.com -> www.example.com
This does NOT work:
example.com/foo.html -> www.example.com/foo.html
Any help would be much appreciated
I think you need to add a \ to escape the dot in your domain (line 2).
As in:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^(.*)\.yourdomain\.com$ [NC]
RewriteRule ^(.*)$ http://www.yourdomain.com/$1 [R=301,L]
...as seen here: http://www.hostingdiscussion.com/promotion-marketing/26083-301-redirect-non-www-www-vice-versa-good-search-engine-optimization-technique.html
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
Here's my rule that work in my prod environment:
# If domain name without "www", add them:
RewriteCond %{HTTP_HOST} ^mydomainname\.(fr|com|net|org|eu) [NC]
# without www => force redirection:
RewriteRule (.*) http://www.mydomainname.%1$1 [QSA,R=301,L]

Resources