Forum Moderators: phranque
RewriteEngine On
RewriteCond %{HTTP_HOST} !^(www\.example\.com)?$
RewriteRule (.*) http://www.example.com/$1 [R=301,L]
Options +SymLinksIfOwnerMatch
RewriteEngine On
RewriteCond %{HTTP_HOST} !^((www|blog)\.example\.com)?$
RewriteRule (.*) http://www.example.com/$1 [R=301,L]
RewriteCond %{HTTP_HOST} ^(www\.blog\.example\.com)?$
RewriteRule (.*) http://blog.example.com/$1 [R=301,L]
RewriteCond %{HTTP_HOST} !^((www|blog)\.example\.com)?$
RewriteRule (.*) http://www.example.com/$1 [R=301,L]
Options +SymLinksIfOwnerMatch
# BEGIN old page redirects
Redirect 301 /test.htm http://www.example.com/testing.htm
# BEGIN errors
ErrorDocument 400 /error.htm
ErrorDocument 401 /error.htm
ErrorDocument 403 /error.htm
ErrorDocument 404 /error.htm
ErrorDocument 500 /error.htm
# Internally rewrite <subdomain>.example.com/<URLpath> to example.com/subs/<subdomain/<URLpath>
RewriteEngine On
RewriteCond $1 !^subs/
RewriteCond %{HTTP_HOST} !^www\.example\.com
RewriteCond %{HTTP_HOST} ^([^.]+)\.example\.com
RewriteRule (.*) /subs/%1/$1 [L]
# Externally redirect client requests for example.com/subs/<subdomain>/<URLpath> to <subdomain>.example.com/<URLpath>
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /subs/
RewriteRule ^subs/([^/]+)/(.*)$ http://$1.example.com/$2 [R=301,L]
# non-www to www if not www or blog
RewriteCond %{HTTP_HOST} !^((www|blog)\.example\.com)?$
RewriteRule (.*) http://www.example.com/$1 [R=301,L]
# BEGIN defaults
DefaultLanguage en-US
AddDefaultCharset UTF-8
AddType 'text/css; charset=UTF-8' css
AddType 'application/javascript; charset=UTF-8' js
# BEGIN expires rules
<IfModule mod_expires.c>
ExpiresActive on
<FilesMatch "\.(jpe?g|gif|png|ico|pdf|swf|flv)$">
ExpiresDefault "access plus 20 days"
Header append Cache-Control "public"
</FilesMatch>
<FilesMatch "\.(js|css|txt)$">
ExpiresDefault "access plus 10 days"
Header append Cache-Control "private, must-revalidate"
</FilesMatch>
<FilesMatch "\.(html?|php)$">
ExpiresDefault "access plus 2 days"
Header append Cache-Control "private, must-revalidate"
</FilesMatch>
ExpiresDefault "access plus 1 hours"
</IfModule>
# BEGIN compress files
<IfModule mod_deflate.c>
<FilesMatch "\.(html?|php|js|css|rss|xml)$">
SetOutputFilter DEFLATE
</FilesMatch>
</IfModule>
# Internally rewrite <subdomain>.example.com/<URLpath> to example.com/subs/<subdomain/<URLpath>
# Externally redirect client requests for example.com/subs/<subdomain>/<URLpath> to <subdomain>.example.com/<URLpath>
Everything ... that is wrong works perfectly fine in FF. In IE10 I get the results above.
Options +SymLinksIfOwnerMatch
RewriteEngine On
# Externally redirect client requests for example.com/subs/<subdomain>/<URLpath> to <subdomain>.example.com/<URLpath>
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /subs/
RewriteCond %{HTTP_HOST} !^blog\.example\.com
RewriteRule ^subs/([^/]+)/(.*)$ http://$1.example.com/$2 [R=301,L]
# Internally rewrite <subdomain>.example.com/<URLpath> to example.com/subs/<subdomain/<URLpath>
RewriteCond $1 !^subs/
RewriteCond %{HTTP_HOST} !^www\.example\.com
RewriteCond %{HTTP_HOST} ^([^.]+)\.example\.com
RewriteRule (.*) /subs/%1/$1 [L]
# non-www to www if not www or blog
RewriteCond %{HTTP_HOST} !^((www|blog)\.example\.com)?$
RewriteRule (.*) http://www.example.com/$1 [R=301,L]
# BEGIN expires rules
ExpiresActive On
ExpiresByType text/html "access"
# Internally rewrite <subdomain>.example.com/<URLpath> to example.com/subs/<subdomain/<URLpath>
<snip>
# non-www to www if not www or blog
Options +SymLinksIfOwnerMatch
RewriteEngine On
# Externally redirect client requests for example.com/subs/<subdomain>/<URLpath> to <subdomain>.example.com/<URLpath>
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /subs/
RewriteCond %{HTTP_HOST} !^blog\.example\.com
RewriteRule ^subs/([^/]+)/(.*)$ http://$1.example.com/$2 [R=301,L]
# non-www to www if not www or blog
RewriteCond %{HTTP_HOST} !^((www|blog)\.example\.com)?$
RewriteRule (.*) http://www.example.com/$1 [R=301,L]
# Internally rewrite <subdomain>.example.com/<URLpath> to example.com/subs/<subdomain/<URLpath>
RewriteCond $1 !^subs/
RewriteCond %{HTTP_HOST} !^www\.example\.com
RewriteCond %{HTTP_HOST} ^([^.]+)\.example\.com
RewriteRule (.*) /subs/%1/$1 [L]
# BEGIN expires rules
ExpiresActive On
ExpiresByType text/html "access"
When accessing www.blog.example.com :
URLMethodResultTypeReceivedTakenInitiatorWaitýýStartýýRequestýýResponseýýCache readýýGapýý
http://www.blog.example.com/GET301text/html332 B2.32 snavigate018778002996
http://www.example.com/subs/blog/GET301text/html324 B63 msnavigate2324063002933
http://blog.example.com/GET200text/html341 B2.80 snavigate23871567800125
KeyValue
ResponseHTTP/1.1 301 Moved Permanently
Locationhttp://www.example.com/subs/blog/
KeyValue
ResponseHTTP/1.1 301 Moved Permanently
Locationhttp://blog.example.com/
IE does not update the url in the browser if the page is not found
The only thing I can complain about now is this url format :
www.blog.test.com
redirects to www.test.com/subs/blog/ then
redirects to blog.test.com
because it has two redirects in one.
[edited by: mihomes at 2:17 am (utc) on Jul 3, 2013]
EDIT - All I'm saying is the other browsers update the change even if there is a 404 while ie just goes straight to the 404. Don't want that misunderstood.
RewriteEngine On
# Externally redirect client requests for example.com/subs/<subdomain>/<URLpath> to <subdomain>.example.com/<URLpath>
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /subs/
RewriteCond %{HTTP_HOST} !^blog\.example\.com
RewriteRule ^subs/([^/]+)/(.*)$ http://$1.example.com/$2 [R=301,L]
# Externally redirect client requests for www.<subdomain>.example.com/<URLpath> to <subdomain>.example.com/<URLpath>
RewriteCond %{HTTP_HOST} ^www\.(.+)$
RewriteCond %{HTTP_HOST} !^www\.example\.com
RewriteRule (.*) http://%1%{REQUEST_URI} [R=301,L]
# BEGIN non-www to www
RewriteCond %{HTTP_HOST} !^(([a-z0-9][-a-z0-9]+)\.example\.com)?$
RewriteRule (.*) http://www.example.com/$1 [R=301,L]
# Internally rewrite <subdomain>.example.com/<URLpath> to example.com/subs/<subdomain/<URLpath>
RewriteCond %{ENV:REQ_SUBDOMAIN} ^$
RewriteCond %{HTTP_HOST} ^((www\.)?([a-z0-9][-a-z0-9]+)\.example\.com)?$
RewriteCond %{HTTP_HOST} !^www\.example\.com
RewriteCond %{DOCUMENT_ROOT}/subs/%2 -d
RewriteRule (.*) /subs/%2/$1 [E=REQ_SUBDOMAIN:%2,L]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /subs/
RewriteCond %{HTTP_HOST} !^blog\.example\.com
RewriteRule ^subs/([^/]+)/(.*)$ http://$1.example.com/$2 [R=301,L]
RewriteRule (.*) http://%1%{REQUEST_URI} [R=301,L]
It seems as if there ought to be a rule for when the host is blog.example.com. The situation may never really occur-- but then, neither should the various /subs/ scenarios. It's just for insurance, right? And if there's a preceding rule for "IS blog.example.com" then you can throw out the "IS NOT" condition in the present rule.
Tiny point here: Either capture and reuse the request-- with parentheses-- OR use {REQUEST_URI} --without parentheses. No point in looking up the same thing twice.
Personally I'd stick with the capture. But then, anything involving %{blahblah} in the target makes me anxious ;)
# Externally redirect client requests for www.<subdomain>.example.com/<URLpath> to <subdomain>.example.com/<URLpath>
RewriteCond %{HTTP_HOST} ^www\.(.+)$
RewriteCond %{HTTP_HOST} !^www\.example\.com
RewriteRule (.*) http://%1%{REQUEST_URI} [R=301,L]
# Externally redirect client requests for www.<subdomain>.example.com/<URLpath> to <subdomain>.example.com/<URLpath>
RewriteCond %{HTTP_HOST} ^www\.(.+)\.example\.com$
RewriteRule ^subs/([^/]+)/(.*)$ http://$1.example.com/$2 [R=301,L]
RewriteCond %{HTTP_HOST} ^www\.(.+)\.example\.com$
Options +SymLinksIfOwnerMatch
# BEGIN rewrites
RewriteEngine On
# Externally redirect client requests for example.com/subs/<subdomain>/<URLpath> to <subdomain>.example.com/<URLpath>
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /sub_ds/
RewriteRule ^sub_ds/([^/]+)/(.*)$ http://$1.example.com/$2 [R=301,L]
# Externally redirect client requests for www.<subdomain>.example.com/<URLpath> to <subdomain>.example.com/<URLpath>
RewriteCond %{HTTP_HOST} ^www\.([a-z0-9-]+)\.example\.com
RewriteRule ^sub_ds/([^/]+)/(.*)$ http://$1.example.com/$2 [R=301,L]
# Externally redirect 'secure' subdomain to https
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^secure\.
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
# Externally redirect non 'secure' subdomain to http
RewriteCond %{HTTPS} on
RewriteCond %{HTTP_HOST} !^secure\.
RewriteRule (.*) http://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
# Externally redirect index.(php|html?) in any location, preserving parameters, to location root
RewriteCond %{THE_REQUEST} !^[A-Z]{3,9}\ /phpsite/
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^/]+/)*index\.(php|html?)
RewriteRule ^(([^/]+/)*)index\.(php|html?)$ http://%{HTTP_HOST}/$1 [R=301,L]
# BEGIN non-www to www
RewriteCond %{HTTP_HOST} !^(([a-z0-9][-a-z0-9]+)\.example\.com)?$
RewriteRule (.*) http://www.example.com/$1 [R=301,L]
# Internally rewrite <subdomain>.example.com/<URLpath> to example.com/sub_ds/<subdomain/<URLpath>
RewriteCond $1 !^sub_ds/
RewriteCond %{HTTP_HOST} !^www\.example\.com
RewriteCond %{HTTP_HOST} ^([^.]+)\.example\.com
RewriteRule (.*) /sub_ds/%1/$1 [L]
# BEGIN expires rules
ExpiresActive On
ExpiresByType text/html "access" [edited by: phranque at 7:54 am (utc) on Jul 13, 2013]
[edit reason] exemplified hostname [/edit]
What I am now having trouble with is redirecting index.(php|html?) to the directory it is in preserving the parameters if any and excluding one directory. Essentially ensuring short links without the index filename.
This causes an infinite loop as it redirects to 'http://blog.example.com/sub_ds/blog/' over and over. Why is it referring to the 'real' location of the file rather than the redirected 'new' location as per the rules above it?
# Externally redirect client requests for example.com/subs/<subdomain>/<URLpath> to <subdomain>.example.com/<URLpath>
# Externally redirect client requests for example.com/sub_ds/<subdomain>/<URLpath> to <subdomain>.example.com/<URLpath> # Externally redirect client requests for www.<subdomain>.example.com/<URLpath> to <subdomain>.example.com/<URLpath>
# Externally redirect client requests for www.[subdomain].example.com/sub_ds/<subdomain>/<URLpath> to <subdomain>.example.com/<URLpath> # Externally redirect client requests for www.<subdomain>.example.com/<URLpath> to <subdomain>.example.com/<URLpath>
RewriteCond %{HTTP_HOST} ^www\.([a-z0-9-]+)\.example\.com$
RewriteRule ^(.*)$ http://%1.example.com/$1 [R=301,L] RewriteRule ^(([^/]+/)*)index\.(php|html?)$ http://%{HTTP_HOST}/$1 [R=301,L]
Pattern is a perl compatible regular expression. On the first RewriteRule, it is matched against the (%-decoded) URL-path (or file-path, depending on the context) of the request.
# Externally redirect index.(php|html?) in any requested location, preserving parameters, to location root
RewriteCond %{THE_REQUEST} !^[A-Z]{3,9}\ /phpsite/
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /(([^/]+/)*)index\.(php|html?)\
RewriteRule .* http://%{HTTP_HOST}/%1 [R=301,L]