Apache modrewrite htaccess redirect - .htaccess

I have tried every variation I can find (found examples on Stack Overflow and elsewhere but they don't work for my situation either).
Here is my revised .htaccess file:
DirectoryIndex index.html
RewriteEngine On
# New Try - apparently does nothing
RewriteCond %{SERVER_PORT} 80
RewriteCond %{HTTP_HOST} ^(www\.)?fubar\.com
RewriteCond %{REQUEST_URI} ^\/?snafu\/?(.*)$
RewriteRule ^(.*) https://snafu.com/%1 [R,L]
# First try - apparently does nothing
RewriteCond %{SERVER_PORT} 80
RewriteCond %{HTTP_HOST} ^(www\.)?fubar\.com
RewriteRule ^\/?snafu\/?(.*)$ https://snafu.com/$1 [R,L]
# this works as expected
RewriteCond %{SERVER_PORT} 80
RewriteCond %{HTTP_HOST} ^(www\.)?fubar\.com
RewriteRule ^(.*)$ https://www.fubar.com/$1 [R,L]
I show above the two ways I have tried to cause
http://www.fubar.com/snafu
to redirect to
http://snafu.com
Neither paragraph does anything.
The last paragraph in the file works as expected, so I know that Apache
is doing it's thing properly.
Clearly the file is working in some cases, but I cannot figure out what is going wrong for the other cases.
As suggested, I replaced all my code above with:
DirectoryIndex index.html
Redirect 301 /snafu https://snafu.com
When I try to browse to http://www.fubar.com/snafu/index.html
I get the Firefox message: The page isn’t redirecting properly
Firefox has detected that the server is redirecting the request for this address in a way that will never complete.
However, the browser URL window displays the desired destination https://snafu.com/index.html
Chrome reports: The snafu.com page isn’t working
snafu.com redirected you too many times.
ERR_TOO_MANY_REDIRECTS
Again, the browser URL window displays the desired destination https://snafu.com/index.html
Internet Explorer says: Not Found
The requested URL /snafu.com/index.html was not found on this server.
Opera says: The snafu.com page isn’t working snafu.com redirected you too many times.
Safari says: Safari can't open the page "https://snafu.com/index.html" because Safari can't establish a secure connection to the server "snafu.com"
I don't know if this is important, but both fubar.com and snafu.com have the same IP address - if I clear the browser cache I can directly navigate to https://snafu.com/index.html without problem, so again it appears that Apache is doing its thing properly.
The snafu.com root directory contains the .htaccess file below, which appears to work as expected:
DirectoryIndex index.html
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteCond %{HTTP_HOST} ^(www\.)?snafu.fubar\.com
RewriteRule ^(.*)$ https://snafu.com/$1 [R,L]
RewriteCond %{SERVER_PORT} 80
RewriteCond %{HTTP_HOST} ^(www\.)?snafu\.com
RewriteRule ^(.*)$ https://snafu.com/$1 [R,L]
Removing this file does not change any of the browser behaviors.
My tentative conclusion is that the simple Redirect 301 /snafu https://snafu.com command is confused because both snafu and fubar have the same IP address. But the ModRewrite should be smarter - I am just not smart enough to come up with the right incantation.
Where am I going wrong? My goal is to make all existing links that reference fubar.com/snafu to be redirected to snafu.com so that search engines are not confused by two different ways to access the same content.

Can you try to add this in your htaccess
Redirect 301 /snafu http://snafu.com

Related

How to implement https redirect to a subdirectory?

I am facing something odd. I have this in my htaccess file under blog directory and I am redirecting only specific folder
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteCond %{REQUEST_URI} blog
RewriteRule ^(.*)$ https://www.admuuv.com/blog/$1 [R,L]
When I write this to browser https://admuuv.com/blog/digital-signage-yazilimindan-alinacak-12-fayda/ it seems ok, but when I delete 's' near 'http' and hit enter forces the url to redirect 'https' but the 'blog' got missed and I get 404 error. Why the blog directory disappears? Why It doesn't just reload the page (by forcing https rule) and keep showing the content? How can I accomplish this? You can observe it from the link also.

Redirection with .htaccess, hiding the original URL

I have a domain, www.mydomain2.com, that I'd like being redirected to www.mydomain1.com/folder_site2/ but I'd like the URL that appears in the address bar being "www.mydomaine2.com" instead of the "www.mydomain1.com/folder_site2/"
After searches in the Apache documentation, I made my .htaccess looking like this:
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www\.)?mydomain2\.com$ [NC]
RewriteCond %{REQUEST_URI} !^/folder_site2
RewriteRule ^(.*)$ /folder_site2/$1 [L]
I first thought it was working fine, as, when I typed "mydomain2.com", I was correctly redirected on "www.mydomain1.com/folder_site2/" with "mydomain2.com" as visible URL.
But it seems it's impossible to reach a sub-folder. Any URL like "mydomain2.com/subfolder/text.html" would reach to a 404 (while it was possible to reach via "mydomain1.com/folder_site2/subfolder/text.html")
What have I done wrong? Thanks for your help!

Redirect all subdomains using .htaccess

What I want to achieve is to redirect any subdomain.mydomain.info to mydomain.info/subdomain using a 301 so that the visitor still sees subdomain.mydomain.info.
After some research I found that I had to set wildcard in my A-Record, did that. Than I went on to create a .htaccess. Below is my entire .htaccess.
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\.domain\.info [NC]
RewriteCond %{HTTP_HOST} ([^.]+)\.domain\.info [NC]
RewriteRule ^(.*)$ /%1/$1 [L]
When I open subdomain.mydomain.info where I know that mydomain.info/subdomain is an existing folder I only get a message telling me that the domain "subdomain.mydomain.info" is unavailable.
My webspace is running a Confixx panel, just if that helps.
What could be going wrong here?
At this point I am guessing that some configuration outside the .htacces need to be made, but no idea what and where.
BIG EDIT:
Revisiting this. Turned out I had to talk to my provider to get some things set up correctly. Still trying to figure this our though.
Current situation: the .htaccess from above gives me a 500. Putting in an R, als was suggested in the comments, will redirect "sd.domain.info" to "domain.info/sd/sd/sd/sd" and result in an error by my browser. The browser says "There is redirect on this page" and give me the option to load it again. The version suggested by Al Kafri Firas also gives me a 500. When I remove the .htaccess any "subdomain.doamin.info" gets redirected to "domain.info" with the URL being changed in the head of my browser.
Still looking to get this working....
Revert all changes you made to your A-Record and use this rules
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\.example\.info$ [NC]
RewriteCond %{HTTP_HOST} ^(www\.)?([a-z0-9-]+)\.example\.info$ [NC]
RewriteRule /%2%{REQUEST_URI} [PT,L]

Redirect all but one page to http from https

For reasons of SEO, I would like to redirect all but one of the pages of my website from https to http. Currently, if you visit the https:// version you are presented with my website and the aforementioned prefix is in the address bar. If you visit the http:// version the https:// prefix is absent. Apparently, as a result, Google treats my site as if it were two identical sites. Not good for SEO.
St.Woland's answer here was very helpful in redirecting almost all the pages of my website from https to http. In fact, it worked like a dream until I tried visiting the one page that needed to have the secure connection.
Before trying his fix, were you to visit the secure basket page the whole page was marked as secure. However, with the recommended .htaccess fix in place, all images and css were flagged as insecure and Chrome refused to load them; the page looked a mess.
It seemed like an article on Best Host Ratings had the solution: add further .htaccess files to the images and css folders and all would be well.
Sadly, this then meant that those pages without the https:// prefix did not load the css or the images.
Please see below the code I put in. Firstly, the .htaccess to redirect all but the basket page from https to http:
RewriteEngine On
RewriteBase /
# Turn SSL on for basket
RewriteCond %{HTTPS} off
RewriteCond %{SCRIPT_FILENAME} \/basket\.php [NC]
RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [R=301,L]
# Turn SSL off everything but basket
RewriteCond %{HTTPS} on
RewriteCond %{SCRIPT_FILENAME} !\/basket\.php [NC]
RewriteRule ^(.*)$ http://%{HTTP_HOST}/$1 [R=301,L]
Secondly, the .htaccess I placed in the images directory:
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteCond %{REQUEST_URI} images
RewriteRule ^(.*)$ https://www.domain.com/images/$1 [R,L]
An obvious solution to this situation might be to just redirect the whole site across to the https version. But, placed at the base of all pages except the basket page is script from Google AdWords that places relevant ads across the internet after users have visited my site. This script is flagged as insecure.
Any help with this issue would be most appreciated.
With kind regards,
Mark

How to redirect a first time visitor using htaccess

How to redirect a first time user to a special landing page using htaccess based on referrer? I mean if they came from another domain then they are the first time visitor?
I am really noob at url rewriting and explanation would be great .
Note: the landing page is nothing but a php script that detects browser. On that page I will use cookie, but need to redirect the user if the referrer is empty or its from another domain.
I suggest this :
RewriteEngine on
RewriteCond %{HTTP_REFERER} !^(www\.)?(https?://)?example\.com[NC]
RewriteCond %{REQUEST_URI} !^/welcome.html [NC]
Rewriterule ^(.*)$ http://example.com/welcome.html [r=307,L]
The first RewriteCond check if referer contains your domain name, and the second check if you are not just redirected by the RewriteRule.
The RewriteRule brings you to the welcome page as a [L]ast rule.
How about redirect the use if his referer is not your domain ?
RewriteEngine on
RewriteCond %{HTTP_REFERER} ^(www\.)?(https?://)?(?!example\.com) [NC]
Rewriterule ^(.*)$ http://example.com/welcome.html [r=307,NC]
That means that the user will be redirected to welcome.html if he writes example.com in the address bar or comes from a link in another site. Once on your site it won't be redirected anymore if he load another page in your site.
P.S. AFAIK you can use cookies in PHP that generates a plain html page see here
Edit: Update tested code
Excuse my reheating the old steak once more.. I would still be interested in knowing if anyone knows the solution to this problem - without using cookies or HTML5 features...
I have read here that the HTTP_REFERER might be blank. Is that why this method of redirecting is not good for this application? I have experimented with this on my server but the closest result working result was being always redirected to my landing page index.htm, which is not desired..
Could this rule interfere with other rewrite rules?
Also, there is an error in the former snippet:
And I think the NC flag in the latter snippet does not make sense. Should it not be L?
RewriteEngine on
RewriteCond %{HTTP_REFERER} !^(www\.)?(https?://)?example\.com[NC]
#missing space after .com and before [----------------here----^
RewriteCond %{REQUEST_URI} !^/welcome.html [NC]
Rewriterule ^(.*)$ http://example.com/welcome.html [r=307,L]
RewriteEngine on
RewriteCond %{HTTP_REFERER} ^(www\.)?(https?://)?(?!example\.com) [NC]
Rewriterule ^(.*)$ http://example.com/welcome.html [r=307,NC]
#Should this flag not be L? ------------------------------^

Resources