mod_rewrite and double slash issue - apache

I applied the following mod_rewrite rule in Apache2 to redirect from non-www to www:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]
However, there's a double slash issue:
When I go to http://www.example.com it
correctly rewrites the URL to
http://www.example.com/
When I go to
http://www.example.com/somepage, it correctly
rewrites the URL to
http://www.example.com/somepage
If I
go to http://example.com, it
rewrites the URL to
http://www.example.com// (double final slash)
If I go to
http://example.com/somepage, it
correctly rewrites it to
http://www.example.com/somepage
Is my configuration good for SEO?

Fixed with:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com$1 [R=301,L]
because $1 by default contains the index path /

RewriteRule ^\/?(.*)$ http://www.example.com/$1 [R=301,L]

Actually, you will always have double slashes due to
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]
combined with the fact that REQUEST_URI (that you are matching on) normally contains a starting slash. What you can try is RewriteRule ^(.*)$ http://example.com$1, and then send a broken HTTP request GET foo HTTP/1.0 and see if Apache deals with it properly.

Putting a slash into your pattern should resolve this issue:
RewriteRule ^/(.*)$ http://www.example.com/$1 [R=301,L]

That is because the root path is /, and you are appending whatever you get in RewriteRule (the first case works fine because it doesn't match the condition so no rewrite is performed).
You can try something like this:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
# for the home page
RewriteRule ^/$ http://www.example.com/ [R=301,L]
# for the rest of pages
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]

Related

Apache redirect subdomain to folder, keep parameters

I have this code in .htaccess :
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !\.(css|gif|ico|jpg|js|png|swf|txt)$
# If empty subdomain, replace with "www"
RewriteCond %{HTTP_HOST} ^example.com$
RewriteRule ^(.*) http://www.example.com/$1 [QSA,L,R=301]
# If subdomain isn't empty and not "www", redirect to "folder"
RewriteCond %{HTTP_HOST} !^www\.example\.com
RewriteCond %{HTTP_HOST} ^(.*)\.example\.com$
RewriteRule (.*) http://www.example.com/%1/$1 [QSA,R=301]
#PAGES REDIRECTION
RewriteRule ^(.*)/register/ /index.php?sub=$1&page=register
RewriteRule ^(.*)/register /index.php?sub=$1&page=register
RewriteRule ^(.*)/lostpass/ /index.php?sub=$1&page=lostpass
RewriteRule ^(.*)/lostpass /index.php?sub=$1&page=lostpass
...
(a rule for wildcard subdmains is already in place and working)
If I browse to http://test.example.com it redirects correctly to http://www.example.com/test but when I try to browse to http://test.example.com/register, it actually redirect to http://www.example.com/test/index.php?sub=http://www.example.com/test&page=register which should redirect to http://www.example.com/test/register
What am I doing wrong here? Thanks in advance!
Try adding the L flag to your second redirect rule, similar to how have it in the first.
RewriteRule (.*) http://www.example.com/%1/$1 [QSA,R=301,L]
It looks like your rewritten URI is passing through to the next rule.
Also, I don't think your first two RewriteCond are in the correct spot.

.htaccess - redirect domain name

I have a domain name www.domainA.com I want to redirect it to domainB in following manner.
www.domainA.com -> www.domainB.com
www.domainA.com/anything -> www.domainB.com/rebrand
How I can do this in htaccess, I have done following code but it redirecting to /rebrand/ only.
RewriteCond %{REQUEST_URI} ^\/
RewriteRule ^\/$ http://www.domainB.com/ [L,R=301]
RewriteCond %{HTTP_HOST} ^domainA\.com$ [NC]
RewriteRule ^(.*)$ http://www.domainB.com/rebrand/ [L,R=301]
URIs that go through rules in an htaccess file has the leading slash stripped off, so you can't match against it. For the second rule, it's matching the / request because the first rule isn't being applied and your regex matches anything or nothing, you can fix that by changing the * to +:
RewriteCond %{HTTP_HOST} ^domainA\.com$ [NC]
RewriteRule ^/?$ http://www.domainB.com/ [L,R=301]
RewriteCond %{HTTP_HOST} ^domainA\.com$ [NC]
RewriteRule ^(.+)$ http://www.domainB.com/rebrand/ [L,R=301]
Redirecting through htaccess is tricky sometimes, there are many ways of implementing this but there is one simple way which worked for me
Options +FollowSymLinks
RewriteEngine on
RewriteRule (.*) [newdomain.com...] [R=301,L]
You can get more info from webmaster world forum

Trailing slash 'bug' in mod_rewrite www-removal snippet

I got a snippet to redirect all www.domain.com requests to domain.com from another SO question:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
Unfortunately it results in an extra trailing slash:
www.domain.com redirects to domain.com// and
www.domain.com/path/ redirects to domain.com//path/
Should I really add an extra rule to remove the trailing slash? Or is there a problem with the above snippet?
Just removing the slash from the third line should work:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1$1 [R=301,L]

apache domain redirect not working as expected

I have the following rewrite rule in apache conf.
RewriteEngine On
RewriteCond %{HTTP_HOST} ^([^\.]+\.[^\.]+)$
RewriteRule ^(.*)$ http://www.%1/$1 [R=301,L]
It redirects http://example.org/?p=18 to http://www.example.org//?p=18. It adds // at the end of domain name. How to fix this?
You have an extra /. You have two choices:
You can avoid putting it in the target:
RewriteRule ^(.*)$ http://www.%1$1 [R=301,L]
Or you can avoid capturing it in the match part:
RewriteRule ^/(.*)$ http://www.%1/$1 [R=301,L]

URL Canonicalization - Apache mod_rewrite

I am probably attempting to take URL Canonicalization a bit too far but here it goes anyway.
Basically I am looking to the following:
301 redirect every base url to http://www.mydomain.com/ some pages are https it should recognize that and continue to use https where already used/requested
301 redirect away any trailing slashes ie http://www.mydomain.com/page/ becomes http://www.mydomain.com/page (I already have a line of code that finds the index.php page - this site is built on Codeigniter)
I Don't want the base url to have the slash stripped that is the only time a slash should be left behind
Find any instances of index.php (in the front middle or end of the url) and 301 redirect them out
ie http://www.mydomain.com/index.php/page/index.php/ becomes http://www.mydomain.com/page
301 redirect any use of my ip address to the actual domain
ie 11.11.111.111/page/index.php would become http://www.mydomain.com/page
Here is what I have so far in my htaccess file in my root directory:
RewriteEngine on
Options +FollowSymLinks
RewriteCond %{REQUEST_URI} ^(.*)(/index\.php)$
RewriteRule ^(.*)index\.php/$ http://www.mydomain.com/$1 [R=301,L]
RewriteCond %{HTTP_HOST} ^(mydomain\.com)(:80)? [NC]
RewriteRule ^(.*)$ http://www.mydomain.com/$1 [R=301,L]
RewriteCond %{HTTP_HOST} ^11\.11\.111\.111$
RewriteRule ^(.*)$ http://www.mydomain.com/$1 [R=301,L]
#This makes sure that the server actually finds the index file although its not in the url
RewriteCond $1 !^(index\.php|images|assets|downloads|css|js|robots\.txt)
RewriteRule ^(.*)$ /index\.php/$1 [L]
I am stuck right now any help would be greatly appreciated!!
Revisions!!
I have made some progress and here is what I have so far
<IfModule mod_rewrite.c>
RewriteEngine on
# index.php to /
RewriteCond %{THE_REQUEST} ^GET\ /.*/index\.(php|html)\ HTTP
RewriteRule (.*)index\.(php|html)$ /$1 [r=301,L]
# index.php to / at the base url
RewriteCond %{THE_REQUEST} ^GET\ /index\.(php|html)\ HTTP
RewriteRule (.*)index\.(php|html)$ /$1 [r=301,L]
# force www.
rewritecond %{HTTP_HOST} ^paolienvelope.com [nc]
rewriterule ^(.*)$ http://www.paolienvelope.com/$1 [r=301,L]
# force no IP
RewriteCond %{HTTP_HOST} ^70.40.204.154
RewriteRule ^(.*) http://www.paolienvelope.com/$1 [r=301,L]
#codeigniter direct
RewriteCond $1 !^(index\.php|images|assets|downloads|css|js|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]
</IfModule>
This successfully forces ip to url
Removes index.php or index.html from the url but correctly directs to the index file despite
makes sure the base url has www.
Still dont have the code to remove the trailing slash from only the request
any help would be appreciated!! Thanks!
RewriteEngine on
# index.php remove any index.php parts
RewriteCond %{THE_REQUEST} /index\.(php|html)
RewriteRule (.*)index\.(php|html)(.*)$ $1$3 [R=301,L]
# force www. (also does the IP thing)
RewriteCond %{HTTP_HOST} !^www\.paolienvelope\.com [NC]
RewriteRule ^(.*)$ http://www.paolienvelope.com/$1 [R=301,L]
# remove tailing slash
DirectorySlash off
RewriteCond $1 !^(index\.php|images|assets|downloads|css|js)
RewriteRule ^(.*)/$ $1 [R=301,L]
# codeigniter direct
RewriteCond $1 !^(index\.php|images|assets|downloads|css|js|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]
Let's try to solve this one point at the time. As said I'm no pro at this yet but any bit helps i guess:
I'll start with 2. because that one seems easier:
#get rid of trailing slashes
RewriteCond %{HTTP_HOST} ^(www.)?mydomain.\.com$ [NC]
RewriteRule ^(.+)/$ http://www.mydomain.com/$1 [R=301,L]
Does this work, instead of what you have?
source:
http://blog.valtersboze.com/2009/06/add-or-remove-trailing-slash-in-url-with-htaccess/