htaccess not updating on the server? - apache

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

Related

Apache Redirect (using .htaccess)

I'm sort of a novice at using mod_rewrite in .htaccess, please forgive me. I've searched far and wide for an answer but perhaps I'm not looking in the right places, or perhaps .htaccess isn't even the right place to do this kind of thing.
Let's say I own two domains: joebloggs.com and bloggs.com.
When you put joebloggs.com into your browser I'd like it to redirect to https://www.bloggs.com/joe.
However, if you go to joebloggs.com/foobar, I'd like to redirect to https://www.bloggs.com/foo/bar.
note: I know this seems counterproductive, going from a shorter URL to a longer one, but the problem is that marketing materials were already disseminated with the URL joebloggs.com/foobar... :-/
The way I've tried to structure this in the .htaccess file hasn't worked thus far.
<IfModule mod_rewrite.c>
RewriteEngine on
#
# redirect joebloggs.com/foobar to https://www.bloggs.com/foo/bar
RewriteCond %{HTTP_HOST} ^joebloggs\.com\/foobar [NC,OR]
RewriteCond %{HTTP_HOST} ^www\.joebloggs\.com\/foobar [NC]
RewriteRule ^(.*)$ https\:\/\/www\.bloggs\.com\/foo\/bar [L,R=301,NC]
#
# redirect joebloggs.com to https://www.bloggs.com/joe
RewriteCond %{HTTP_HOST} ^joebloggs\.com$ [NC,OR]
RewriteCond %{HTTP_HOST} ^www\.joebloggs\.com$ [NC]
RewriteRule ^(.*)$ https\:\/\/www\.bloggs.com\/joe [L,R=301,NC]
</IfModule>
When I put joebloggs.com into a browser, it redirects correctly to https://www.bloggs.com/joe - but when I try to go to joebloggs.com/foobar, the URL remains intact and thus goes to a 404 page (because it doesn't exist).
What am I doing wrong? Am I not doing this the optimal way, or not using .htaccess/mod_rewrite as it was intended?
Sorry for being long-winded, I hope I gave enough information. Thanks for any help and time spent on this!
The first set of rules is entirely useless. Update the second set to:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?joebloggs\.com$ [NC]
RewriteRule ^/?$ https://www.bloggs.com/foo [R=301,L,NC,QSA]
RewriteCond %{HTTP_HOST} ^(www\.)?joebloggs\.com$ [NC]
RewriteRule ^foo(.*)$ https://www.bloggs.com/foo/$1 [R=301,L,NC,QSA]

RewriteCond "except for directory" failing in complex rule set

I need to force everything except IE8 to HTTPS and IE8 specifically to HTTP
(it's temporarily - hence the 302 - its stupid, but there are legit business reasons).
I want all of this to ignore the /api/ directory as the app that utilizes these sadly doesn't follow redirects.
The following is working, IE8 detection is working. Everything is except /api/whatever is still being redirected.
I'd really appreciate any advice or an explanation of why this is not working.
# make sure mod_rewrite is ON
RewriteEngine On
# force staging and live to SSL
RewriteCond %{HTTPS} !=on
# Unless its IE 8
RewriteCond %{HTTP_USER_AGENT} !compatible;\sMSIE\s8\.0 [NC]
RewriteCond %{HTTP_HOST} ^(www\.|staging\.)?example\.com [NC]
# Skip the API
RewriteCond %{REQUEST_URI} !^/api/.*
# 301 Permanent
RewriteRule .* https://%{SERVER_NAME}%{REQUEST_URI} [L,R=301]
# force IE8 to Non-SSL
RewriteCond %{HTTPS} on
RewriteCond %{HTTP_USER_AGENT} compatible;\sMSIE\s8\.0 [NC]
RewriteCond %{HTTP_HOST} ^(www\.|staging\.)?example\.com [NC]
# Skip the API
RewriteCond %{REQUEST_URI} !^/api/.*
# 302 Temporary
RewriteRule .* http://%{SERVER_NAME}%{REQUEST_URI} [L,R=302]
Don't see anything wrong, though assuming that you've cleared your browser's cache, you could try a different approach and include an explicit pass-through at the top of your list of rules:
RewriteRule ^api/ - [L]
You can add that right under RewriteEngine On and get rid of the /api conditions.
The RewriteCond %{REQUEST_URI} !^/api/.* was working, the problem was a later rewrite routing everything through a front controller /index.php was causing the htaccess to reparse.
Changing the rule to RewriteCond %{REQUEST_URI} !^(/api/|/index.php) solved the problem. the front controller, index.php isn't callable directly anyway, so it's not an issue.

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>

godaddy .htaccess RewriteRule

I'm trying to set up a .htacess file on Godaddy webhosting ( apache-linux ofcourse, not IIS ). But i`m stuck with a problem:
RewriteEngine On
Options +FollowSymLinks
Rewriterule ^templates/.*$ - [PT]
Rewriterule ^controllers/.*$ - [PT]
Rewriterule ^.*$ index.php [NC,L]
I`m getting an Internal Server Error for line:
Rewriterule ^.*$ index.php [NC,L]
I don't know how to fix this, i've tried everything I know... basically I want to send anything that comes to index.php where a bootstrapper is set. This is working on any hosting i`ve ever tried, but godaddy seems to have problems with this: ^.*$ any help would be appreciated.
This rule
Rewriterule ^.*$ index.php [NC,L]
looks OK, but make sure that there are no spaces between the flags
i.e [NC, L] should be changed to [NC,L].
You can try the equivalent to see if it makes a difference (you don't need NC because it already matches any request)
Rewriterule .* index.php [L]
If that is not the issue, then it is likely the rules before that cause it to fail. Comment them both out and see if it works, but add a RewriteCond as below
Options +FollowSymLinks
RewriteEngine On
#if its not already index.php
RewriteCond %{REQUEST_URI} !index\.php$ [NC]
Rewriterule .* index.php [L]
If those rules are the real cause, then I would like to know what was your intent with those rules, and can they be expressed a different way i.e. if the intent was to rewrite all content except that in the templates or controllers directory, then that could be achieved by
#if request is not for templates or controllers directory
RewriteCond %{REQUEST_URI} !^/(templates|controllers)/ [NC]
Rewriterule .* index.php [L]

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

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.