Can anyone help me with this:
RewriteCond %{HTTP_HOST} !^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://www.$1 [R=301,L]
What I am trying to do is create a rewrite rule that sends you to the www version of a site if you try to connect using the non www version.
The condition works but the rules doesn't, it sends me to http://
Can anyone suggest hoe I can fix this.
I was expecting $1 = everything in the condition above between ^ and $
Thanks
Don't use HTTP_HOST, it's evil.
Do this:
RewriteCond %{SERVER_NAME} !^www\. [NC]
RewriteCond %{SERVER_NAME} (.*)
RewriteRule (.*) http://www.%1/$1 [R=301,L]
Where %1 matches the grouping from a previous RewriteCond.
Related
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.
I want to do the following with apache (mod rewrite).
if the user requests http://hostname.tld/index.php/folder/subfolder i want it to redirect (with a R=301) to http://hostname.tld/folder/subfolder.
if the user requests http://hostname.tld/folder/subfolder the request should internally be rewritten to index.php/folder/subfolder.
To prevent an endless redirect the first rule should check for %{THE_REQUEST}. The problem here is that I am unable to append "folder/subfolder" with a regex. How should I do this?
For the second rule I have this (and seems to work).
RewriteCond %{HTTP_HOST} hostname.tld [NC]
RewriteRule ^(.*)$ index.php [QSA,L]
The first one is still a problem.
I think the first one should be something like
RewriteCond %{THE_REQUEST} (.*)index.php(.*) [NC]
RewriteRule /index.php/$ http://hostname.tld/$1 [R=301,QSA,L]
But that is not really it.
The first should be.
RewriteCond %{HTTP_HOST} ^hostname\.tld$ [NC]
RewriteCond %{THE_REQUEST} index\.php [NC]
RewriteRule ^index.php/(.*)$ http://hostname.tld/$1 [R=301,L]
I also see that your second rule redirects http://hostname.tld/folder/subfolder to http://hostname.tld/index.php (not http://hostname.tld/index.php/folder/subfolder). But as long as that works it's fine, as this it also prevents the redirect loop.
But just in case, here is the solution to add the folder/subfolder part:
RewriteCond %{HTTP_HOST} ^hostname\.tld$ [NC]
RewriteCond $1 !^index\.php
RewriteRule ^(.*)$ index.php/$1 [QSA,L]
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
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]
I would like to redirect to https using mod_rewrite only if certain conditions are met:
If the URL does NOT contain the word 'administrator'
AND the URL DOES contain the string 'xyz' (in any part of the URL, including the querystring)
This does not seem to work:
RewriteCond %{REQUEST_URI} xyz [NC,OR]
RewriteCond %{QUERY_STRING} xyz [NC]
RewriteCond %{REQUEST_URI} !administrator [NC]
ReWriteCond %{HTTPS} != on
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [NC,R,L]
Try this rule:
RewriteCond %{THE_REQUEST} !administrator
RewriteCond %{THE_REQUEST} xyz
RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R,L]
Testing the request line in THE_REQUEST is easier as it contains both the path and query. But make sure your xyz is not part of the method or HTTP version.
I ended up using a coding solution as I could not get it to work with mod_rewrite. :(