Using .htaccess to redirect domain.co.uk/index.html to www.domain.co.uk - apache

I have noticed that search engines have been crawling both the domain.co.uk and www.domain.co.uk versions of a web site that I've recently developed.
Using .htaccess I have been able to setup http 301 redirects so that:
http://domain.co.uk is redirected to http://www.domain.co.uk
and
http://www.domain.co.uk/index.html is redirected to http://www.domain.co.uk
However:
http://domain.co.uk/index.html does not get redirected to http://www.domain.co.uk as I would expect. Instead the redirect goes to: http://www.domain.co.uk/http://www.domain.co.uk/
The contents of my .htaccess are (obviously I have replaced my domain to simplify matters):
Options +FollowSymlinks
RewriteEngine on
RewriteCond %{http_host} ^domain.co.uk [nc]
RewriteRule ^(.*)$ http://www.domain.co.uk/$1 [R=301,nc]
RewriteCond %{THE_REQUEST} ^.*/index.html
RewriteRule ^(.*)index.html$ http://www.domain.co.uk/$1 [R=301,L]
I know I am probably missing something obvious but please could someone help me get the final redirect to http://www.domain.co.uk working as expected?

The first part of your rule set is working fine, the missing www. is added correctly. For the second part you only need a simple rule to remove the index.html without using any additional condition:
Options +FollowSymlinks
RewriteEngine On
RewriteCond %{HTTP_HOST} ^domain.co.uk [NC]
RewriteRule ^(.*)$ http://www.domain.co.uk/$1 [R=301,NC]
RewriteRule ^index.html$ http://www.domain.co.uk/ [R=301,L]
I think this should do the trick.

Related

htaccess not updating on the server?

I'm at a bit of a loss. I'm working on a website so I have created a temporary construction.html page that I want to redirect all IPs to other than my own. Here is what my .htaccess looks like:
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://www.example.org/$1 [R,L]
ErrorDocument 404 http://www.example.org/construction.html
Options +FollowSymlinks
RewriteCond %{REMOTE_ADDR} !^12\.34\.567\.891
RewriteCond %{REQUEST_URI} !/construction.html$ [NC]
RewriteRule .* /construction.html [R=302,L]
What's strange is that it seemed to work initially, but my IP still gets rejected most of the time, but I haven't found a pattern. Sometimes I can access the root index.html if I explicitly navigate to it.
What's even stranger is that if I comment out those last four lines, I still get redirected to the construction.html page.
I've made sure to clear the cache of the browser, and that doesn't seem to do anything. Perhaps it is a syntax thing? My understanding is that the changes should be instantaneous.
Based on your shown attempts, could you please try following. Please make sure that your index.html, consutuctions.html and .htaccess rules files are present in root folder. Please make sure to clear your browser cache before testing your URLs.
Options +FollowSymlinks
RewriteEngine On
##Block access to index.html here..
RewriteRule ^index\.html/?$ - [NC,L]
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)/?$ https://www.example.org/$1 [NE,R=302,L]
RewriteCond %{REMOTE_ADDR} !^12\.34\.567\.891
RewriteCond %{REQUEST_URI} !/construction\.html/?$ [NC]
RewriteRule ^ /construction.html [R=302,L]
ErrorDocument 404 http://www.example.org/construction.html

apache Rewrite rule appends /html directory for no reason

I have a domain foo.tech.
I want to use a new domain footech.io instead.
The redirect also has to make sure all the URLs work.
E.g foo.tech/bar goes to footech.io/bar
Here is my .htaccess file:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^foo.tech [NC]
RewriteRule ^(.*) http://footech.io/$1 [R=301,L]
For some reason, it decides to add /html at the end of my domain.
So now if I visit foo.tech it will redirect to footech.io/html
If I visit foo.tech/bar it will redirect to footech.io/html/bar
Please help.
Update:
I think the /html comes from the $1
I've tried to make the rewrite rule as follows:
RewriteRule ^(.*) http://footech.io/$1/$1 [R=301,L]
going to foo.tech leads to footech.io/html//html/
going to foo.tech/bar leads to footech.io/html/bar/html/bar
final update:
I made it work now using this:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^foo.tech [NC]
RewriteRule ^html/(.*) http://footech.io/$1 [R=301,L]
This seems to fix it
RewriteEngine On
RewriteCond %{HTTP_HOST} ^foo.tech [NC]
RewriteRule ^html/(.*) http://footech.io/$1 [R=301,L]

Redirect a.com/a.html to b.com/a.html

I need to redirect a.com/a.html to b.com/a.html htaccess Apache. I have several subdomains that are not being redirected to the b.com(which is fine). and the example below is only the first domain that needs to be redirected. Three other domains are needed to be redirected to b.com as well with all of their subpages as a.com/a.html to b.com/a.html. Any hints?
RewriteCond %{HTTP_HOST} ^(www\.)?nexus21\.com$ [NC,OR]
RewriteCond %{HTTP_HOST} ^tvlift\.com$ [NC]
RewriteRule ^ http://www.tvlift.com%{REQUEST_URI} [L,NE,R=301]
I would do each subdomain redirect separately. That way it is cleaner and more maintainable. If you are redirecting few known files then name them directly.
RewriteEngine on
RewriteRule "^/foo\.html$" "bar.html" [R]
If there are lots of file or you will be adding in future then use a pattern
RewriteEngine on
RewriteRule "^/docs/(.+)" "http://new.example.com/docs/$1" [R,L]
See this page
http://httpd.apache.org

Should be a simple 301 redirect

What I think should be simple is just not working.
I have switched domains
Old URL example:
digital.photorecommendations.com/recs/2015/01/big-zoom-field-review/
New URL example:
photorec.tv/2015/01/big-zoom-field-review/
Really just switching domain and dropping the recs folder from the URL
Using http://htaccess.madewithlove.be/ to test and the outputs the correct URL
Options +FollowSymlinks
RewriteEngine on
RewriteBase /recs
RewriteCond %{HTTP_HOST} !^www\.digital.photorecommendations\.com$ [NC]
RewriteRule ^recs(.*) http://photorec.tv/$1 [L,R=301]
When I place this in the htaccess file I get 404 errors on all the pages except the home page. The htaccess file is inside the /recs folder. I have also tried it in the root directory of digital.photorecommendations.com and I get no results at all.
Any suggestions?
Thanks!
You have wrongly used negation in RewriteCond and regex also needs a fix. Use this rule in /recs/.htaccess:
Options +FollowSymlinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www\.)?digital\.photorecommendations\.com$ [NC]
RewriteRule ^(index\.php)?$ http://photorec.tv/ [L,R=301,NC]
RewriteCond %{HTTP_HOST} ^(www\.)?digital\.photorecommendations\.com$ [NC]
RewriteRule ^(.+)$ http://photorec.tv/recs/$1 [L,R=301,NC,NE]

Drupal redirect from installation subdirectory without creating a loop

I am working on a Drupal site that has hard coded links in lots of the content (I didn't create the site). Unfortunately that means the site has to be in a subdirectory such as "www.example.com/drupal" or the links will break. I have sucessfully redirected the site root to the subdirectory and hid it using the following code in the site root htaccess file:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule (.*) drupal/$1 [L]
RewriteCond %{HTTP_HOST} !^www\..+$ [NC]
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
</IfModule>
However if I go to www.example.com/drupal the subdirectory still shows up.This means that www.example.com and www.example.com/drupal have the same content which is bad for SEO. Is there some way to make /drupal redirect to root without causing a loop? I have tried just about every posted in other threads and none of them work.
You need to do the redirect before rewriting the URL. You also need to check that you're actually rewriting to a legitimate resource.
swap the rules around, the redirect should happen first
Add a check to the drupal rule
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\..+$ [NC]
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
RewriteCond %{REQUEST_URI} !^/drupal
RewriteRule (.*) drupal/$1 [L]
</IfModule>