Apache2 301 Redirect Not Working - apache

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]

Related

.htaccess forward 'www.w.' to 'www.'

I have a weird issue where Google is indexing several of my site's pages as 'www.w.example.com' which is causing issues with my security certificate.
While I'm looking for a solution, I'd like to redirect this using .htaccess but I can't seem to get it to work.
Currently I have:
RewriteRule ^(.*)www\.w\.(.*)$/$ https://www.$1/$2 [R=301,L]
But it doesn't seem to work..
I have multiple domains for this site so ideally it needs to redirect to the correct domain e.g. https://www.example1.com or https://www.example2.com
It is a bad SEO practice to have the same content available on more than 1 URLs.
You need to decide on the best URL you would like to use and then do a 301 redirect of the others to it.
You need to use a RewriteCond for matching host name:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.w\.(.+) [NC]
RewriteRule ^ http://www.%1%{REQUEST_URI} [R=301,L,NE]
However you might still get SSL cert warning because cert negotiation happens before mod_rewrite rules are invoked.

301 Redirect not working in 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!

mod_rewrite redirect specific .asp pages back to original server

I need to redirect traffic for a section of a redeveloped site back to the original site with apache mod_rewrite rules. I need to redirect all requests starting with http://www.example.com/page.asp back to the original site http://www.original.com/page.asp with the query string or anything following page.asp intact.
This seems simple enough, however I have had no luck with mod_rewrite generators or documentation on the web. My latest stab at the problem looks like this:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^http://www.example.com$ [NC]
RewriteRule ^page\.asp(.*)$ http://www.original.com/page\.asp$1 [R=301,NC]
I appreciate any insight into correcting this mod_rewrite rule. Other redirects are working fine.
I was able to get the rewrite to function properly using:
RewriteRule ^/page(.+) http://www.original.com/page$1 [R=301,NC,L]
I found this apache doc helpful: Resource Moved to Another Server

.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]

.htaccess redirect doesn't hide url

my .htaccess in the root folder includes the following lines :
Options +FollowSymlinks
RewriteEngine on
RewriteRule ^(.*)\.htm$ http://example.com/?city=$1 [NC]
when I open the address http://example.com/bla.htm, my browser doesn't hide the GET values specified in the .htaccess, it redirects me to ?city=bla. eventhough I'm not using the [R] switch. This has always worked for me before (as I remember, haven't dealt with htaccess in a while). What's wrong here ?
When you redirect to an entire URL, it doesn't do URL rewriting (you can't exactly rewrite URLs on someone else's website).
Assuming both URLs are on the same server, you need to do something like
RewriteRule ^(.*)\.htm$ index.php?city=$1 [NC]
Also, I'd recommend getting into the habit of using the [L] switch whenever you can - it helps avoid bugs when you have a lot of URLs to rewrite.