I'm looking for help with a chain redirect issue, please.
Desired behaviour: example.com/foo?amp redirects to example.com/bar
Current behaviour: example.com/foo?amp redirects to example.com/foo then redirects to example.com/bar
This is a Wordpress site. Htaccess has the following rule, which doesn't appear to be solving the chain redirect:
RewriteCond %{QUERY_STRING} amp
RewriteRule ^/foo$ /bar [L,R=301]
My htaccess kung foo is quite weak, so I'm fully expecting that I've made a mess there!
Any help appreciated :)
You missed just a tiny detail to get things working:
RewriteEngine on
RewriteCond %{QUERY_STRING} amp
RewriteRule ^foo$ /bar [L,R=301]
The pattern in the rewriting rule is matched against the relative path of the URL (foo, not/foo), when implemented in a distributed configuration file (".htaccess").
It really makes sense to read the documentation of the tools you are using for such details. As typical for OpenSource software the documentation for the apache http server and its module is of excellent quality and comes with great examples.
You want to start with here:
http://httpd.apache.org/docs/current/howto/htaccess.html
https://httpd.apache.org/docs/current/mod/mod_rewrite.html
One could optimize above rule a bit:
RewriteEngine on
RewriteCond %{QUERY_STRING} ^amp$
RewriteRule ^/?foo/?$ /bar [R=301,END]
And you should check whether you can implement such a rule in the actual http server's host configuration instead of using distributed configuration files...
Related
I want to redirect some URLS on the old site to specific destination on the new site and the rest of the URLS to the home page. So far I have tried the following:
Use Redirect 301 /permalink https://newsite.com/permalink1/ for all the links that need to go to a specific page on the new site
For the rest, I added a generic rule
RewriteCond %{HTTP_HOST} ^oldsite\.com$ [NC]
RewriteRule ^(.*)$ https://newsite.com [R=301,L]
but this redirects all the pages to the home page of the new site.
Then I tried to replace point 2 above with RedirectMatch
RedirectMatch 301 .* https://newsite.com
but this tries to find an exact match on the new site. oldsite.com/xyz goes to https://newsite.com/xyz
Also, some of the old site URLs have parameters, oldsite.com/a=?b=123 how can I redirect such parameters to the new site homepage.
This probably is a straight forward approach:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^oldsite\.com$
RewriteRule ^/?permalink1old(/.*)$ https://newsite.com/permalink1new$1 [R=301,QSA]
RewriteCond %{HTTP_HOST} ^oldsite\.com$
RewriteRule ^/?permalink2old(/.*)$ https://newsite.com/permalink2new$1 [R=301,QSA]
RewriteCond %{HTTP_HOST} ^oldsite\.com$
RewriteRule ^ https://newsite.com/ [R=301]
So you redirect the old permalinks on the old host name to their new counterpart on the new host name, appending anything that might follow in the path of the initial old permalink and you append the query string to preserve arguments.
If the permalinks never contain anything additional in their paths then you can simplify those redirections, obviously:
RewriteCond %{HTTP_HOST} ^oldsite\.com$
RewriteRule ^/?permalink1old/?$ https://newsite.com/permalink1new [R=301,QSA]
It is a good idea to start out with a 302 temporary redirection and only change that to a 301 permanent redirection later, once you are certain everything is correctly set up. That prevents caching issues while trying things out...
This implementation will work likewise in the http servers host configuration or inside a dynamic configuration file (".htaccess" file). Obviously the rewriting module needs to be loaded inside the http server and enabled in the http host. In case you use a dynamic configuration file you need to take care that it's interpretation is enabled at all in the host configuration and that it is located in the host's DOCUMENT_ROOT folder.
And a general remark: you should always prefer to place such rules in the http servers host configuration instead of using dynamic configuration files (".htaccess"). Those dynamic configuration files add complexity, are often a cause of unexpected behavior, hard to debug and they really slow down the http server. They are only provided as a last option for situations where you do not have access to the real http servers host configuration (read: really cheap service providers) or for applications insisting on writing their own rules (which is an obvious security nightmare).
TL;DR: I want to redirect https to http on all pages except for admin/login, where I want the exact opposite to happen. I also want www redirected to bare domain name. (UPDATE: Check Update 3 for the answer)
As is probably clear from the title, I have a Wordpress blog hosted on OpenShift for free. I have a custom domain bought from GoDaddy. I'm using cloudflare so I can have free SSL.
Here's my configuration:
CloudFlare DNS:
CloudFlare Page Rules:
This is what worked best. I actually wanted to have this rule:
*ghostlessmachine.com/* -> https://ghostlessmachine.com/$1
But I ran into even more problems like that, even though it seems to be pretty much what I'm supposed to do according to this CloudFlare article. Actually, initially I wanted to only force SSL in admin pages, but I didn't even know how to attempt that. I thought of using two page rules, like this:
*ghostlessmachine.com/* -> http://ghostlessmachine.com/$2
*ghostlessmachine.com/wp-* -> http://ghostlessmachine.com/$2
But I had no luck.
Here's my OpenShift configuration:
When I write ghostlessmachine.com in my address bar, it correctly takes me to https:.... I have shared a link, however (https://ghost...), and one person has reported not being able to access it. I couldn't reproduce locally.
When I try www.ghost..., I get:
This webpage has a redirect loop
ERR_TOO_MANY_REDIRECTS
Does anybody have any idea what I'm doing wrong? I've lost track of how many different configurations I've tried, but nothing seems to work.
Thanks!
UPDATE
OK, so following the advice in the comment I managed to get the situation a bit better. Still it's counter intuitive for me how the article I initially linked to just didn't get the job done while the other SO question did. So here's what I've changed:
Deleted the www.ghost... alias from OpenShift.
Changed CloudFlare's CNAME record from www -> blabla.rhcloud.com to www -> ghostlessmachine.com
Created this Page Rule: www.ghostlessmachine.com/* -> http://ghostlessmachine.com/$1
Now both ghost... and www.ghost... work and take me to http://ghost.... However, if I type https://ghost..., it also works without redirecting me to simple http. This is a problem.
I tried using this Page Rule instead:
ghostlessmachine.com/ -> http://ghostlessmachine.com/$2
So that I got https://, http://www, www, everything redirected to http://ghost..., but it doesn't work. I can't access my blog anymore and whatever address I try I get ERR_TOO_MANY_REDIRECTS.
UPDATE 2
Here's my full setup after all suggestions:
htaccess:
wp-config.php:
CloufFlare:
Result:
https -> http on non-admin/login pages: WORKING ✓
Trying to access admin/login pages: ERR_TOO_MANY_REDIRECTS
Update 3
This did the trick:
I still don't understand why this works and the rest doesn't though. This was basically a series of rather blind trial and error with some input from Allen here and Simon in the CloudFlare support page. In any case, all my requirements are respected now, thanks!
make sure following in your wp-config.php file:
define('FORCE_SSL_ADMIN', true);
define('FORCE_SSL_LOGIN', true);
look over here: Force non-WWW but force https across all of wordpress & the entire domain
for redirect everything else to non-https, you can add following into your root .htaccess file, before the wordpress rewrite:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTPS} =on
RewriteCond %{REQUEST_URI} !^/wp-admin.*
RewriteCond %{REQUEST_URI} !^/wp-login.*
RewriteCond %{HTTP_REFERER} !^https://.*
RewriteRule ^(.*)$ http://%{HTTP_HOST}/$1 [L]
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
Update:
CloudFlare's Page rule has following "Page rule priority is determined by their position in the list. If multiple rules match a URL, rules at the top take higher priority. "
let's see what happens before:
request to https://www.ghostlessmachine.com/wp-admin hit the first
rule, match found, then it goes to
http://www.ghostlessmachine.com/wp-admin!
now here comes http://www.ghostlessmachine.com/wp-admin, first rule,
no rewrite, goes down to 3rd rule, oops, it needs goto
https://www.ghostlessmachine.com/wp-admin!
this is how the loop comes
I have a wordpress site that was hacked and hundreds of urls were created that no longer exist after I cleaned the site up. I have noticed a lot of these urls contains a specific keyword as displayed below
http://www.capecrossfit.com/kamagra-holland/ - I need to rewrite all urls containing the word kamagra to http://www.capecrossfit.com/ - I have tried all the recommendations on near similar requests on this site with no joy, can anyone please advise how this can be done with htaccess?
Pace this rule as very first rule (just below RewriteEngine On line):
RewriteEngine On
RewriteCond %{THE_REQUEST} /kamagra-holland|buy-kamagra-europe|kamagra-super-jelly [NC]
RewriteRule ^ / [L,R=301]
I am trying to create a permanent htaccess redirect (301) from all my domain extensions into the appropriate subdirectories. The "rules" are as follow:
Redirect belgian website to its subdirectory on the main website:
from: www.example.be
to: www.example.com/befr/
Of course I would like to preserve the url parameters (if any) of the "from". Globaly, if someone entered the first url it should redirect to the second url (langage subdirectory in the main website).
I'm using wordpress and I'm hosting on a plesk I've read many things here but I'm stuck, thank you very much in advance for your help
PS: I've tried that but it doesn't work
RewriteCond %{HTTP_HOST} ^(www\.)?example.be$ [NC]
RewriteRule ^(.*) http://www.example.com/befr/$1 [L,R]
After reading your question, your code should be working (with informations you gave).
If it's not, here are some points to check:
1. Make sure mod_rewrite is enabled and htaccess can be executed (Apache config).
2. Your htaccess has to be in root folder (where example.be is forwarded).
3. About your htaccess' code:
since you're using Wordpress, make sure your rule is on top of other rules
don't forget to escape . (second one) in RewriteCond (otherwise it doesn't mean the same) even if it works that way
replace R flag (302 by default) by R=301 if you want a 301 redirect
Your code now looks like this
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?example\.be$ [NC]
RewriteRule ^(.*)$ http://www.example.com/befr/$1 [R=301,L]
# your other rules (and Wordpress' default rule) here
I switched blogging software (MT to WordPress) on a site and need to redirect requests to http://www.domain1/atom.xml to http://www.domain1.com/feed/atom.
I was using a simple Redirectmatch rule, but realized that it was also redirecting requests made to another site (domain2), that is is hosted by the server, in a subdirectory of domain1, which I do not want to happen (its feed is still at http://www.domain2.com/atom.xml).
How do I get the redirect to only occur for domain1?
I was trying to do the following, but it didn't work.
RewriteCond %{HTTP_HOST} ^www\.domain1\.com [NC]
RewriteRule ^/atom\.xml$ http://www.domain1.com/feed/atom [L,R=301]
Am I close?
Thanks,
Rich
If you don't do any rewriting for domain2 then a quick fix would be to create a .htaccess file inside its root folder and disable rewriting with RewriteEngine off.
Otherwise you are on the right path with the RewriteCond, it should do the trick. Have you tried adding $ at the end (RewriteCond %{HTTP_HOST} ^www\.domain1\.com$ [NC]) / any misspelling / www. vs no www.?
I figured it out, but I'm not sure why exactly this works. I moved my .htaccess to be:
RewriteCond %{HTTP_HOST} ^www\.domain1\.com [NC]
RewriteRule ^atom\.xml$ http://www.domain1.com/feed/atom [L,R=301]
I removed the slash in front of "atom" in the RewriteRule.
I would think I should have the slash, as I'm tyring to redirect http://www.domain1.com/atom.xml .
It's at the root of the domain...
Oh well. Can anyone explain why this works? Is the string passed to the pattern matching not contain the starting slash?
Thanks,
Rich