Mod_rewrite redirect not working as intended (working in htaccess checker) - apache

I'm trying to redirect users accessing https://www.mywebsite.com/index.php?p=home to https://www.mywebsite.com/. I have already added the code below to my public_html's .htaccess file and have tested it on an htaccess checker.
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_URI} ^/index\.php$ [NC]
RewriteCond %{QUERY_STRING} ^p=home$ [NC]
RewriteRule .* https://%{HTTP_HOST}/ [R=301,L,QSD]
Based on my code, do you have any idea why the redirect is not working even though the htaccess checker says it should be working.

You could try following Rules in your htaccess rules file. Please don't trust htaccess checker sites they don't give correct results. Have your htaccess file with following rules and make sure you clear your browser cache before testing your URLs.
RewriteEngine ON
##Redirection external rules here.
RewriteCond %{THE_REQUEST} \s/index\.php\?p=home\s [NC]
RewriteRule ^ https://%{HTTP_HOST} [R=301,L]
##Rewrite internal rule here.
RewriteRule ^/?$ index.php?p=home [L]

Related

Rewriting URLs with htaccess, multiple parameters

I'm trying to rewrite something like this:
https://mywebsite.com/pages/article.html?id=1&title=Title-Goes-Here
into
https://mywebsite.com/pages/article/1/Title-Goes-Here
Using this Rewrite Rule
RewriteEngine on
RewriteRule ^article/([0-9]+)/([0-9a-zA-Z_-]+)$ article.html?id=$1&title=$2 [NC,L]
However, when I try this code in https://htaccess.madewithlove.com/ it gives me
This rule was not met.
Also tried it on my website htaccess file with no result. I don't know where is the problem.
Please don't create OR test rules on online sites, they are NOT trust worthy, so kindly test these rules into your localhost OR apache.
With your shown samples/attempts, please try following htaccess rules. Considering that you are hitting URL https://mywebsite.com/pages/article.html?id=1&title=Title-Goes-Here in browser AND you want to redirect it to URL https://mywebsite.com/pages/article/1/Title-Goes-Here in browser.
Please make sure to clear your browser cache before testing your URLs.
RewriteEngine ON
##External redirect in browser rules here....
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{THE_REQUEST} \s/([^/]*)/([^.]*)\.html\?id=([^&]*)&title=(\S+)\s [NC]
RewriteRule ^ /%1/%2/%3/%4? [R=301,L]
##Internal rewrite to html file rules here....
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]*)/([^/]*)/([^/]*)/([^/]*)/?$ $1/$2.html?id=$3&title=$4 [QSA,NC,L]

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]

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]

.htaccess rewrite to simultaneously change domain and remove path

My URL structure is currently as follows:
http://domain.com/folder/filename (CURRENT)
I want to change this so that I can use the following URL instead:
http://sub.domain.com/filename (NEW)
So accessing the CURRENT or the NEW url, should load the file located at the CURRENT url, but show the NEW url in the address bar. It should only apply to the "/folder/" path.
sub.domain.com is a mirror of domain.com, ie. they share the same file system and root directory.
This is what I have so far:
Options +FollowSymLinks
RewriteBase /
RewriteCond %{HTTP_HOST} ^(www.)?domain.com$ [NC]
RewriteCond %{REQUEST_URI} ^/folder/?(.*)$ [NC]
RewriteRule ^(.*)$ http://sub.domain.com/$1 [R=301,L]
This is working, but is missing the rule to remove the "/folder/" from the path. I've tried combining multiple RewriteRule's with no luck. Any ideas? Thanks.
UPDATE: Thanks again #Gerben - I understand what your rules are doing now, but the second one isn't working for me. I suspect because it's conflicting with some other rewrite rules, in particular those of WordPress, which are lower down in my .htaccess file:
# BEGIN WordPress
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
# END WordPress
Because of this the page ends up in a redirect loop, ie (from Chrome):
"The webpage at http://sub.domain.com/folder/index.php has resulted in too many redirects." - while the url I was originally trying to access was, for example, http://sub.domain.com/page
Any ideas?
Try:
RewriteCond %{HTTP_HOST} ^(www.)?domain.com$ [NC]
RewriteRule ^(folder/)?(.*)$ http://sub.domain.com/$2 [R=301,L]
This will redirect everything to sub.domain.com, and remove the /folder part of the URI if it is there. If not, it redirects and leaves the URI untouched.
RewriteCond %{THE_REQUEST} /folder/
RewriteRule ^folder/(.*)$ http://sub.domain.com/$1 [R=301,L]
RewriteCond %{HTTP_HOST} ^sub\.domain\.com$ [NC]
RewriteCond %{REQUEST_URI} !^/folder/
RewriteRule ^(.*)$ folder/$1 [L]
# WordPress rules here
edit the second R=301 should not have been there
But this won't work, as wordpress has no way of knowing you want folder. You could add the Proxy flag to the rewrite, but then you need to change the rule above to not redirect on this internal proxy request.

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.