mod_rewrite redirect specific .asp pages back to original server - apache

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

Related

htaccess removing a specific address with a query string

I have a site where the issue has come up where I am getting a 404 error on a specific site URL with a query string. This isn't I a site I work on frequently so I am not sure if any queries should still be used. I tried this myself and it didn't work.
Redirect 301 /category.aspx?id=MISC34 /
I want the 301 direct to be specific to www.example.com/category.aspx?id=MISC34 to go to the homepage. This did not work for me. It is on Wordpress if that matters.
I am not that well versed in htaccess. Any help would be apperciated.
You can use in your .htaccess:
RewriteEngine on
RewriteCond %{QUERY_STRING} (^|&)id=MISC34(&|$) [NC]
RewriteRule ^category\.aspx$ /? [L,NC,R=301]

Rewrite old mediawiki URL pattern to short URL format

We have been running a mediawiki installation for some years now. During an upgrade to a newer version, we decided to switch to the short url pattern wikipedia uses. This is working fine.
So our config file now looks like this
# Enable the rewrite engine
RewriteEngine On
# Short url for wiki pages
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} !-f
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} !-d
RewriteRule ^(.*)$ %{DOCUMENT_ROOT}/index.php [NC]
As specified in the documentation for mediawiki.
But we have some old URLs floating around other places, which we would like to support still.
The old URL would look like this:
wiki.domain.com/wiki/Index.php/articlename
Now this URL looks like this:
wiki.domain.com/wiki/articlename
But using the old URL, now just gives a page not found.
We have tried using an online htaccess tester to find the correct RewriteRule. And we get it to work there. But not when using it in on our apache server config.
Tried with this rule, right after RewriteEngine On:
RewriteRule ^(.*?)index\.php(/|$) /$1 [R=301,NC,NE]
Which is supposed to redirect the user to a new url. But we still get the page not found error, and the URL still looks the same.
Is our new rewrite rule wrong, or might there be something else wrong in the apache config. Will provide the config file if need be.
Ended up with this rule:
RewriteRule ^(.*?)index\.php\/(.*) $1$2 [L,NC,R=301]
Select the parts before and after index.php and store them using (). Using $1 and 2 to create a new URL string. I'm sure there are better solutions, but this works too.

RewriteRule Redirects paths not working

I moved my website to a new server with a new CMS so I had to make a lot of 301 Redirects. 'Normal' 301 redirects didn't recognize the url path of my old urls so I tried to make RewriteRules, this is what it looks like now:
Options -MultiViews
RewriteEngine On
RewriteBase /
RewriteRule ^Category http://www.example.com/category [R=301,L]
RewriteRule ^Category/Subcategory http://www.example.com/category-subcategory [R=301,L]
The first RewriteRule works, but as soon as there is a second path in the old url (the second example) the redirect will point to the main cateagy and not the subcategory. So it's basically ignoring the url paths...
Try to invert your rules, or to add a $ at the end of the first one :
RewriteRule ^Category$ http://www.example.com/category [R=301,L]
RewriteRule ^Category/Subcategory http://www.example.com/category-subcategory [R=301,L]
Explanation : Category/Subcategory is also matching the first rule, and as you have use a L flag in the first one, Apache will just use this first rule and don't bother to look further.
For general purpose solution, quoting from apache rewrite guide:
Move Homedirs to Different Webserver Description:
Many webmasters have asked for a solution to the following situation:
They wanted to redirect just all homedirs on a webserver to another webserver. They usually need such things when establishing a newer webserver which will replace the old one over time.
Solution:
The solution is trivial with mod_rewrite. On the old webserver we just
redirect all /~user/anypath URLs to http://example.com/~user/anypath.
RewriteEngine on
RewriteRule ^/~(.+) http://example.com/~$1 [R,L]
In your case URL structure has changed so ôkio's suggestion would work.

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

url issues, need to redirect 3 urls but the codes i've tried arent working

I know nothing about code sadly but i'm hoping that you can help. I've tried asking my web designer but he claims he doesn't know what a 301 redirect is and have noticed his own site has canonicalization issues.
I need to redirect the 3 urls below to www.mydomain.co.uk
www.mydomain.co.uk/index.html
mydomain.co.uk/index.html
mydomain.co.uk
I have never used ftp before, but have downloaded filezilla tonight so i can try fix this and have made a .htaccess file as my site didnt seem to have one.
I have browsed (almost) the whole internet looking for answers to this solution and have tried some codes suggested in previous posts on here but they didn't seem to do anything.
If you need to know, i think my server is apache.
I hope your site isn't built using HTML.. However, using your .htaccess you could try:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.mydomain\.co\.uk [NC]
RewriteRule ^(.*)$ http://www.mydomain.co.uk/ [R=301,L]
This should be sufficient enough to force the www If you need to force the index.html you would do:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.mydomain\.co\.uk [NC]
RewriteRule ^(.*)$ http://www.mydomain.co.uk/$1 [R=301,L]
It's been a while, but both of these should sufficient to do what you want...
I think it's time to find a new developer, because the one you have sounds like a complete idiot.