301 Redirect not working in Apache - apache

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!

Related

Htaccess 301 redirect with variables

I am having some problems. I switched over from one e-commerce system to another a while back and this changed the url structure of some of the pages. I was fine with this and set up some rewrite rules. Somehow this has stopped working and I am not sure why.
I need to change
/product/{Category-Name}/{URL} to /{URL}
/ecategory/{Category-ID}/{URL} to /{URL}
/manu/{URL} to /{URL}
/news/{category-name}/{URL} to /blog/{URL}
I am using
RewriteRule ^/?(product|ecategory)/([^\/]+)/(.+?)\$ /$3 [R=301,L]
RewriteRule ^/?(manu)/(.+?)\$ /$2 [R=301,L]
RewriteRule ^/?(news)/([^\/]+)/(.+?)\$ /blog/$2/$3 [R=301,L]
RewriteRule ^/?(newscategory)/([^\/]+)/(.+?)\$ /blog/$3 [R=301,L]
RewriteRule ^/?([0-9]+)/(.+?)\$ /$2 [R=301,L]
Bu this is not working now and I don't have enough knowledge of htaccess redirects to work out what is wrong. I have been making changes and looking online but I feel like I am going in circles and this is very confusing.
Please, can anyone give me some advice?
I solved it by removing the / before the $. This was my mistake from when I think the URLs used to have .html at the end.

Apache2 301 Redirect Not Working

I have attempted to read various documentation, as well as other answers online, and I cannot find a solution to my problem.
Here is my issue:
I have a permanent www to non-www redirect setup to point traffic from www.domain.com to domain.com
This works, but I am having issues when trying to visit specific urls. For example:
We have three PDFS that we would like people to be able to download via:
domain.com/paper/somepdfname.pdf
When i visit the url like this it works, but if I try to visit something like this:
www.domain.com/paper/somepdfname.pdf
The browser gets redirected, and strips one of the slashes out, resulting in "the site cannot be reached error". This is the url I end up with in the browser:
domain.compaper/somepdfname.pdf
I think its pretty clear that I need to somehow make sure there is a forward slash put before the paper, but I do not know how to do this. Below you will find my .htaccess directive for handling the redirect:
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1/ [R=301,L]

htaccess file ignoring www redirect

I finally got around to using htaccess to properly redirect my www pages to non-www urls, but I can't seem to get it to work in any way I've tried. I believe something that was included in the htaccess before I changed it is causing the problem.
I added
#www redirect
RewriteCond %{HTTP_HOST} www\.(.+)$
RewriteRule (.*) http://%1/$1 [R=301]
in this http://pastebin.com/vAQ45Mky, but it seems to ignore it completely. My night spent searching for answers has turned up nothing useful, so thanks for any help you can give!
That's a big file, and I don't know what you've tested, but the first thing I'd try would be adding an L flag:
RewriteRule (.*) http://%1/$1 [L,R=301]
Without that flag, it'll keep trying the other rules, and apply those that hit. At which point I'm not sure if it is even defined what happens when first applying an absolute-URL rewrite, then (one or more) path rewrites, but I would not be surprised to see the hostname part of the first rewrite lost.

301 Redirect only working on Refresh

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.

.htaccess mod_rewrite 301 redirect with nested exceptions?

I need a little help with my .htaccess before I deploy it!
I want to 301 redirect almost everything from elementalthreads.com to ethreads.com, excluding blog/wp-content/uploads, and /pommo.
Am I doing this right?:
RewriteEngine on
#exclude old uploads folder and /pommo
RewriteCond %{REQUEST_URI} !^/(blog/wp-content/uploads|pommo) [NC]
RewriteRule (.*) http://ethreads.com/$1 [R=301,L]
Will that transfer canonical pagerank?
Here's where I know I need help:
The old site has a wordpress blog, which I've cloned on the new domain. I'd love to preserve the permalinks, which are almost 1:1, eg:
http://www.elementalthreads.com/blog/ethreads-now-on-amazon-com/ redirects to
http://ethreads.com/ethreads-now-on-amazon-com/ (note /blog/ is missing here)
And the blog index http://www.elementalthreads.com/blog/ should redirect to http://ethreads.com/blog/, which seems like an exception to the above rule, since "/blog/" should only be preserved here?
I'm stumped about how to regEx or otherwise define these last two conditions/rules. Any help would be most appreciated!
That looks correct to me. However, you should not put this live without checking it, there really is nothing preventing you from being able to test it. One thing to bare in mind is that browsers can cache 301 response codes so when testing you should use [R,L] as your flags. Once you are happy add the [R=301,L] back in before deployment.
OK for points (1) & (2)
# only redirect the blog direcotry
RewriteRule ^blog/?$ http://ethreads.com/blog/ [NC,R=301,L]
# redirect all sub folders of blog to the new domain
RewriteRule ^blog/([\w-])/?$ http://ethreads.com/$1/ [NC,R=301,L]