Hi sorry if the answer is somewhere else but I did search and didn't run across this problem. I want to use a 301 to redirect www to non-www and this is the code I'm using.
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
It kinda of works.. If you type in www.exaple.com it doesn't work but if you refresh the page THEN it does work.
Anyone know whats going on here?
Thanks!
Rule works fine, your issue is using R=301 (permanent redirect) which tells browsers to cache redirected URLs aggressively.
When you force refresh in browser then only browser request goes to web server otherwise it serves the content from its cache.
As a general practice one should avoid using R=301 (Permanent Redirect) while testing out mod_rewrite rules.
Related
I want to redirect from one domain to a new domain. At the same time, the URL structure has changed.
Old: https://www.olddomain.com/parentpage/oldtitle/
New: https://www.newdomain.com/newtitle
This is wordpress, and I placed this code above the Wordpress stuff, as well as tested it here: https://htaccess.madewithlove.be/
I tried this, which doesn't work:
Redirect 301 /parentpage/title https://www.newdomain.com/newtitle
Also, when testing it at https://htaccess.madewithlove.be/, I do have this redirect:
Redirect 301 /parentpage https://www.newdomain.com/parentpage
The tester would skip my preferred redirect above, and use this one, leaving me with this, which does not exist:
https://www.newdomain.com/parentpage/oldtitle
Even when I place the preferred redirect above this one. I need both, unfortunately.
Have also tried the following RewriteRules (not all at the same time)
ReWriteRule https://www.olddomain.com/parentpage/oldtitle/ https://www.newdomain.com/newtitle
ReWriteRule /parentpage/oldtitle/ https://www.newdomain.com/newtitle
ReWriteRule "https://www.olddomain.com/parentpage/oldtitle/" "https://www.newdomain.com/newtitle"
I think it has something to do with that second forward slash separating the parentpage name and page title, but I can't figure out how to fix it.
In RewriteRule it wouldn't match http or https in it, you may try following.
please make sure you clear your browser cache before testing your URLs.
RewriteEngine ON
RewriteCond %{HTTP_HOST} ^(?:www\.)olddomain\.com [NC]
RewriteCond %{REQUEST_URI} ^/parentage/oldtitle/?$ [NC]
RewriteRule ^(.*)$ https://www.newdomain.com/newtitle [R=301,L]
I'm experiencing a frustrating problem with Apache rewrite rules and a 301 redirect.
We have an extensive list of RewriteRules, all of which work properly. Now I want anyone who goes to the root of our site to be redirected to another site. Here are the applicable rewrite rules in Apache:
RewriteRule "^/index.html" "http://newsite.somewhere.com" [R=301,L]
RewriteRule "^/special-programs" "http://newsite.somewhere.com/special-programs/" [R=301,L]
RewriteRule "^/prospective/" "http://newsite.somewhere.com/adms" [R=301,L]
RewriteRule "^/current/careers/" "http://newsite.somewhere.com/careers" [R=301,L]
RewriteRule "^/current/" "http://newsite.somewhere.com/current" [R=301,L]
Each of these redirects work except for the first one, the root of the site. It redirected using the defaults. However, as soon as I add [R=301,L] it simply stops redirecting all-together, while the other redirects continue to work properly. (A 301 redirect is a requirement; we can't use the default 302.)
I will add that there are many (dozens) redirects above these, but none of them involve the root of the site.
I've tried moving the RewriteRule to the top, to the bottom, and a few other places. No change. I'm reluctant to comment out the dozens of Rewrite rules above but will do so if last resort.
The rewrite rules are in a conf file under /etc/httpd/conf.d.
This seems like a straightforward scenario, so it is unclear to me why the 301 redirect isn't working. If anyone can provide some insight, it would be much appreciated. Thank you very much!
I'm using .htaccess to make the membership renewal pages on my Drupal site secure, but I'm having trouble switching back to regular http when the user navigates away from that page. The page that should be secure is:
www.example.com/renew
I want everything else to use http, and I'm trying the following redirects to achieve this:
# Renewal page should be secure. Redirect.
RewriteCond %{HTTPS} off
RewriteRule ^renew$ https://www.example.com/renew [R,L]
# If user leaves the Renewal page, make sure we're no longer secure. Redirect.
RewriteCond %{HTTPS} on
RewriteCond %{REQUEST_URI} !^renew$
RewriteRule (.*) http://www.example.com%{REQUEST_URI} [R,L]
I got the first redirect working fine. When I added the second, unsecure redirect, it breaks the first redirect -- Firefox says there's a redirect loop.
Can you tell me what I'm doing wrong here? Thanks in advance.
%{REQUEST_URI} is a variable that always starts with a /. Due to this, the second condition of the second rule will always be true, as %{REQUEST_URI} will never be equal to ^renew$. To fix this, change that condition to:
RewriteCond %{REQUEST_URI} !^/renew$
I would like to rewrite the URL of one of my pages from
http://www.mydomain.com/some/application/page.html
to
http://www.mydomain.com/apply
I believe this code will work. But in 301 redirects, you often see [R=301,L] or some version of that appended to the end of the rewrite rule - is the code below the best way to perform the redirection and will Google understand it?
RewriteEngine On
RewriteRule ^([^/]*)\.html$ /some/application/page.html?=$1 [L]
I think you want to do this:
The URL that your users see (even google):
http://www.mydomain.com/apply
to
http://www.mydomain.com/some/application/page.html
the internal(actual) URL.
then I would suggest you to go this:
RewriteEngine On
#condition to redirect
RewriteCond %{REQUEST_URI} ^/?apply/?$
RewriteRule ^/?apply/?$ /some/application/page.html [L]
The flag [L] ( Flag L docs ) signifies RewriteEngine to stop rewriting any rules further. This will not perform a permanent redirection.
You don't need a [R=301] flag here. 301 is for permanent redirection. Use the short URL everywhere.
To my knowledge, you will need [R=301,L]to do a redirect properly. Apache2 by default will use a 302 redirect so if it is a permanent redirect you should force R=301, as you noted. The documentation for RewriteRule is unclear if [L] alone will always perform a 301 redirect. Be safe, tell apache exactly what to do :-).
Current situation
RewriteRule ^$ /index.php?page=Portal [R=301,L]
When a user comes to the website and goes to the "root" url of the domain (RegExp "^$") he's redirected to /index.php?page=Portal
That's working.
Now we have "index.php?page=Portal" in the google index and we have tons of links to that page on various locations all over the internet.
Intended new situation
We want the portal page to show up on the root url - no redirect. That's no problem... Just remove the redirect:
RewriteRule ^$ /index.php?page=Portal [L]
Now we also want the old url to redirect to the new location, and that's where I fail but can't see why:
RewriteCond %{QUERY_STRING} ^page=Portal$
RewriteRule ^index.php$ http://www.jacatu.de/? [R=301,L]
As soon as I do this I end up in a redirect loop:
(When I change to 302 in .htaccess I see 302 redirects, so the loop really seems to be caused by mod_rewrite)
But why? All rules are marked as last [L] - so I think I can rule out that rule 2 triggers rule 1.
I enabled logging as suggested by Jacek Prucia and in fact it looked like having [L] in the URL doesn't stop execution. Both rules were processed.
I now changed the first rewrite to
RewriteRule ^$ /index.php?page=Portal&int=1 [L]
so that it doesn't match the RewriteCond of the internal rewrite so theoretically my problem is solved. It would be nice to know, though, why it did what it did. :)