Htaccess 301 redirect with variables - apache

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.

Related

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.

URL Redirecin Vhost

How can i redirect something.some.* to something.some in vhos
I tried various rewrite rules but none worked so no use pasting them..
My guess is that you are trying to redirect a hostname? I'm not entirely sure as to what you are trying to do.
If my assumption is correct then something like this should work for you:
RewriteCond %{HTTP_HOST} ^something\.some\..*$
RewriteRule (.*) http://something.some/$1 [R=301,L]

.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 URL rewrite for both php and pdf files

I know this comes up often on SO but I'm struggling to understand bits of it and hoping somebody can help.
I need to take the following:
http://www.xyz.com/news.room/showstory.php?storyid=123456
and create
http://www.xyz.com/newsroom/123456
http://www.xyz.com/news.room/
and create
http://www.xyz.com/newsroom/
http://www.xyz.com/files/pdf/uk/Wk51.pdf
and create
http://www.xyz.com/newsroom/convert/Wk51
in addition xyz.co.uk and xyz.org should also be rewritten to xyz.com and all should report back to the search engines as permanent redirects
I'm trying to do this in a htaccess file, that also has a standard CodeIgniter rewrite rule in it to remove the query strings.
Can anyone help me? I've tried several things but can't get anything that works for everything! Also i've spent five hours on this and am slowly pulling my hair out!
Any help, pointing in the right direction etc.. would be appreciated.
Cheers
#redirect any domain other than example.com, to example.com
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !^example.com$ [NC]
RewriteRule ^(.*)$ http://example.com/$1 [R=301,L]
RewriteRule ^newsroom/$ /news.room/
RewriteRule ^newsroom/([0-9]+)$ /news.room/showstory.php?storyid=$1
RewriteRule ^newsroom/convert/([^/]+)$ /files/pdf/uk/$1.pdf

How To Remove Redundent ?p=1 From URL

So I had an issue where my pagination would append a ?p=1 to the end of a URL when moving from page 2 back to page 1. With the issue corrected so those links no longer get served, I do have to try and find a way to do a 301 redirect from those URLs to the proper URL so that the search engines can update those links.
I am on an Apache server, and I would like to be able to use RedirectMatch 301 to do this, however I do not think that it plays well with query strings.
RedirectMatch 301 (.*)?p=1 http://mydomain.com/$1
does not do the trick
I believe the solution is going to end up being that I use RedirectCond and Rewrite statements to make this work, however I do not know how to get this to redirect.
Can someone help me out with this? What I have so far gives me Internal Server Errors so obviously I am not on the right track yet.
RewriteCond %{REQUEST_URI} (.*)$
RewriteCond %{QUERY_STRING} ^?p=1$
RewriteRule ^.*$ http://devserver/$1 [L,R=301]
If someone can even point me toward a good tutorial on how to set this up would be helpful too.
RewriteCond %{QUERY_STRING} (^|&)p=1(&|$)
RewriteRule . http://devserver%{REQUEST_URI}? [L,R=301]