When I use
RewriteEngine On
RewriteCond %{HTTP_HOST} ^example.com
RewriteRule (.*) http://www.example.com/$1 [R=301,L]
in my httpd.conf file, why is my site redirecting to www.example.com//
(www.example.com//file.html). Why are there two slashes ?
I think this should be:
RewriteCond %{HTTP_HOST} !^www\.example\.com [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]
EDIT:
The above RewriteCond was probably overkill - it was intended to only match urls that are not preceded by www. However this should work too:
RewriteCond %{HTTP_HOST} ^example.com
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]
Like David Chan mentioned, the ^(.*)$ is what you were missing. The ^ and $ are special characters in Regular Expressions. Here is a link that explains regex string anchors: http://www.regular-expressions.info/anchors.html
Also, here is a link that can explain the mod_rewrite syntax in more detail: http://httpd.apache.org/docs/current/mod/mod_rewrite.html
Related
My website currently has two version to google, https://www.example.com and a https://example.com This is apparently not good for SEO as its leaking all my seo 'juice' away. I have already told google to use just the www version around 5 weeks ago but come results on google still show the non-www version. I was wondering if anyone could help with my .htaccess to get the redirect right to just www.
The htcaccess file is copied below:
RewriteEngine on
# Force SSL
RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond $1 !^(blog)
RewriteCond %{HTTP_HOST} ^`.com$ [NC,OR]
RewriteCond %{HTTP_HOST} ^www.`.com$
RewriteCond %{REQUEST_URI} !public/
RewriteRule (.*) /public/$1 [L]
RewriteRule ^(.+)$ /index.php [L]
For a simple redirect you can use:
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule .* https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
If you want to also force HTTPS:
RewriteCond %{HTTPS} off
RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
Note: This block goes above the www. rewrite rules to make sure both rules take hold
I need to Rewrite every Dir to https, www and trailing slash and for SEO i need every redirect with only one 301.
The first twoo are solved, but the third doesn't work if de URW comes with www (it returns a 302 if a URL exist, and then write de trailing slash), but i've a rule to handle that case, can you see something wrong?
ssl.conf:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.
RewriteCond %{REQUEST_URI} !^/(.*)\.(.*)
RewriteCond %{REQUEST_URI} !^(.*)/$
RewriteRule .* https://www.%{HTTP_HOST}%{REQUEST_URI}/ [L,R=301]
RewriteCond %{HTTP_HOST} !^www\.
RewriteCond %{REQUEST_URI} ^/(.*)\.(.*)
RewriteCond %{REQUEST_URI} ^(.*)/$
RewriteRule .* https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{HTTP_HOST} ^www\.
RewriteCond %{REQUEST_URI} !^/(.*)\.(.*)
RewriteCond %{REQUEST_URI} !^(.*)/$
RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI}/ [L,R=301]
It was my fault, the rules above works fine, the problem was with my hosts file that hadn't the www entry to test.
I leave the Rule for help anyone if needed.
I would like to redirect from example.com/profile.php?UserName=xxxx to xxxx.example.com
xxxx contains a-z or 0-9.
I have tried bunch of codes to do it (some from this site) but none of them worked as I wanted it to be. Here is my current code:
RewriteEngine on
#this should redirect example.com/profile.php?UserName=x to x.site.com
RewriteCond %{HTTP_HOST} !www.example.com$ [NC]
RewriteCond %{HTTP_HOST} ^([a-z0-9-_]+).example.com [NC]
RewriteRule (.*) %1/$1 [QSA,L]
#if link does not contain subdomain add www
RewriteCond %{HTTP_HOST} ^example.com
RewriteRule (.*) http://www.example.com/$1 [R=301,L]
You need to put all of your redirect rules before your internal routing rules. The rules with the R flag redirect.
RewriteEngine on
# This redirects the browser to include the "www"
RewriteCond %{HTTP_HOST} ^example.com [NC]
RewriteRule (.*) http://www.example.com/$1 [R=301,L]
# This redirects the browser when profile.php is directly accessed
RewriteCond %{THE_REQUEST} \ /+profile\.php\?UserName=([a-z0-9-_]+)
RewriteRule ^ http://%1.example.com/? [L,R]
# this internally routes the XXX.example.com to the profile.php script
RewriteCond %{HTTP_HOST} !www.example.com$ [NC]
RewriteCond %{HTTP_HOST} ^([a-z0-9-_]+).example.com [NC]
RewriteRule ^$ /profile.php?UserName=%1 [QSA,L]
Since there was not a good answer here.. In order to rename a php file and use the subdomain extention.. I had to edit my DNS settings.. This tutorial helped :http://www.mediacollege.com/internet/server/apache/mod-rewrite/subdomains.html
I'm trying to redirect my website about dentists in London from the non-www to the www version and I'm using the following code on the .htaccess of my server, but it doesn't work. What do I miss?
Thank you
RewriteEngine On
### re-direct to www
RewriteCond %{http_host} !^www.topdentists.co.uk [nc]
RewriteRule ^(.*)$ http://www.topdentists.co.uk/$1 [r=301,nc]
Try this:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
This is more general and will work for any site, not just the one you are currently working on. Taken from: Dense13.com (explanation of the code is in the second comment, underneath the article)
The reason why I think your code didn't work is because the RewriteCond uses a regular expression, but you didn't escape the dot (which is a regex-character).
To properly handle any potential https requests, as well as any strange characters, something like this is usually recommended:
# From http://stackoverflow.com/a/4958847/1078583
RewriteCond %{HTTP_HOST} !^$
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTPS}s ^on(s)|
RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
I have a blog installed in www.foo.com/wp/ and would like all requests that go to any page that doesn't start with /wp/ to be redirected to www.foo.com/wp/ (without preservation of the URL).
E.g.:
www.foo.com/bar/baz/hello.gif > www.foo.com/wp/
Also, at the same time, I need the URL to have www. added if it doesn't (which might be where my rules are getting confused)
At the moment, I have:
RewriteCond %{HTTP_HOST} !^www.foo.com [NC]
RewriteCond %{HTTP_HOST} !^$
RewriteRule ^/(.*) http://www.foo.com/$1 [R=permanent]
RewriteRule ^(?!wp)/(.*)$ http://%{HTTP_HOST}/wp/($2) [R=permanent]
But I think this is currently completely broken.
Help much appreciated.
RewriteCond %{HTTP_HOST} !^www.foo.com [NC]
RewriteCond %{HTTP_HOST} !^$
RewriteRule (.*) http://www.foo.com/$1 [R=permanent,L]
RewriteRule ^(?!wp/)(.*) http://%{HTTP_HOST}/wp/$1 [R=permanent]
The path portion of the URL being matched never has a leading slash, and you don't need to anchor a .*.
L modified prevents rewriting from continuing after you arrive at your first redirect.
Negative-lookbehind (?!) is not a capturing construct, and you substitute in captured patterns by writing $1, not ($1).
Edit: Apparently for OP the last rule doesn't work, failing to exclude the cases that begin with /wp/, which makes no sense to me but whatever. Here's an attempt at a workaround:
RewriteCond %{HTTP_HOST} !^www.foo.com [NC]
RewriteCond %{HTTP_HOST} !^$
RewriteRule (.*) http://www.foo.com/$1 [R=permanent,L]
RewriteCond %{REQUEST_URI} !^/wp/
RewriteRule (.*) http://%{HTTP_HOST}/wp/$1 [R=permanent]
Try these rules:
RewriteCond %{HTTP_HOST} !^www.exmaple.com [NC]
RewriteCond %{HTTP_HOST} !^$
RewriteRule ^ http://www.example.com%{REQUEST_URI} [R=301,L]
RewriteCond %{THE_REQUEST} ^GET\ /wp/
RewriteRule ^wp/(.*) /$1 [R=301,L]
RewriteRule !^wp/ wp%{REQUEST_URI} [L]
The first rule is for the host name. The second is to remove the /wp path prefix externally. And the third is to add the prefix again for internal redirection.