What is the htaccess rule to do this? - .htaccess

What would be the htaccess rule to achieve the following please ?
I have a bunch of deleted URLs which i would like to simply 301 redirect to the homepage.
The URLS have this format, showthread.php?tid=405
Any parameter additional can also be redirected to the homepage.
eg. showthread.php?tid=405&pid=112
I would like to redirect by range, to save myself making individual entries, eg tid=405-420
They must all be redirected to the homepage (example.com) and my site is running in https.
I know i need to do something like this
RewriteCond %{REQUEST_URI} ^/showthread\.php$
RewriteCond %{QUERY_STRING} ^id=([0-9]*)$
RewriteRule https://www.example.com [R=301,L]
but short of stuying htaccess rules for hours, i just can't figure it out. I'd appreciate some help. Thanks

Related

302 redirect url with parameters

In htaccess file the following works
redirect 302 /old.php http://somesite.com/new.php
but following fails
redirect 302 /old.php?this=that http://somesite.com/new.php
I think it's because the second version contains a query string. How can we redirect URLs like that?
Please note the destination URL format is different so it cannot be an automated rule, so I need to write the custom URLs that users will be redirected to.
I found a similar question with replies here, but the proposed solutions do not work:
.htaccess not able to redirect url
Thank you
You may use this rule as topmost rule in site root .htaccess of old domain:
RewriteEngine On
RewriteCond %{QUERY_STRING} (?:^|&)this=that(?:&|$) [NC]
RewriteRule ^old\.php$ http://somesite.com/new.php? [R=301,L]

How to 301 redirect example.com/http://cnn.com/page to example.com/cnn/page?

I'm setting up a project where users can get information about specific pages.
How can I redirect urls in htaccess that look like:
http://www.example.com/http://www.cnn.com/article1.html
to:
http://www.example.com/cnn/article1.html
It is ok if the htaccess specifies cnn and not all dynamic web addresses.
I tried using 301 directory redirects but I think the "http://" part of it is confusing it.
I tried using 301 directory redirects like the following code, but the http:// part is still confusing it:
RewriteRule ^http://youtube.com/(.*)$ /youtube/$1 [L,R=301]
RewriteRule http://www.example.com/http://youtube.com/(.*)$ /youtube/$1 [L,R=301]
This code below works properly to redirect example.com/youtube.com/watch?v=videoid to example.com/youtube/watch?v=videoid:
RewriteRule ^youtube.com/(.*)$ /youtube/$1 [L,R=301]
but if the user types in a link with http:// in it then the server cannot currently redirect properly.
Even better, if you can remove the /http:// and /https:// altogether from http://example.com/http://url.com and http://example.com/https://url.com to redirect to http://example.com/url.com/ then the redirect code that I have working in the youtube example will fix the issue for all of the domains.
How can I do this?
Thank you!

URL Redirect and Append #Hashtag

My client has gone through a rebrand and has changed their primary URL. I need to redirect all traffic that visits old URLs and send them to the same page on the new URL and ideally append a #hashtag... the hashtag will trigger an overlay explaining the redirect. Both sites are on the same Drupal codebase and thus use the same .htaccess file.
So if someone visits:
http://oldsite.com/abc/def
They should be redirected to:
http://newsite.com/abc/def/#hashtag
Any help would be greatly appreciated.
something like this?
Though I'm no expert when it comes to htaccess trickery.
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\.newdomain\.com
RewriteRule (.*) http://www.newdomain.com/$1/#hashtag [R=301,L]

Need .htaccess assistance

We have an existing site, let's call it ourdomain.com. We moved content over from a site that is shutting down, let's call it legacydomain.com. We pointed the legacy domain at our server to the /public_html/ folder.
What I need is an .htaccess that will redirect legacydomain.com or legacydomain/anything-here to ourdomain.com/legacydomain/ with nothing else appended to the URL.
However, I also need a few specific URLs to redirect to certain destinations, and they don't really follow a pattern. For example:
legacydomain.com/something.html to ourdomain.com/legacydomain/something.html
legacydomain.com/another.html to ourdomain.com/legacydomain/folder/another.html
This is what I have tried:
RewriteCond %{HTTP_HOST} ^www\.legacydomain\.com$ [NC]
RewriteRule (.*) http://www.ourdomain.com/legacydomain/$1 [R=301,L]
Redirect 301 /something.html http://www.ourdomain.com/legacydomain/another.html
It mostly works, but if I visit legacydomain.com/anything-here it doesn't even attempt to rewrite, it just keeps the domain the same and gives a 404. And also I have a feeling that even if it did work, something like legacydomain.com/anything-here/more-stuff would get rewritten as ourdomain.com/legacydomain/anything-here/more-stuff which I don't want.
Only other thing in the .htaccess is rewriting non-www to www, and the standard WordPress stuff. Any guidance would be greatly appreciated. Everything above should have an http:// and www in front for the examples, but it wouldn't let me post that many "links".
For each specific rewrite you would need two lines, as follows. Depending on your existing config you may need to add a slash at the beginning of the RewriteRule in front of something.html if this doesn't work.
RewriteCond %{HTTP_HOST} legacydomain.com
RewriteRule something.html http://ourdomain.com/legacydomain/something.html [R=301,L]
Then you would use a catch-all for everything else.
RewriteCond %{HTTP_HOST} legacydomain.com
RewriteRule (.*) http://ourdomain.com/legacydomain/ [R=301,L]
Personally, I would go for the simplest solution which doesn't use mod_rewrite. First, just redirect the specific pages to wherever they need to go.
Redirect 301 /something.html http://ourdomain.com/legacydomain/something.html
Redirect 301 /another.html http://ourdomain.com/legacydomain/another.html
Then, simply redirect everything else to the base URL.
RedirectMatch 301 (.*) http://ourdomain.com/legacydomain/
These must be put in your .htaccess file before the RewriteEngine on statement.

batch .htaccess 301 redirect

I'm moving servers and cleaning up the url's a bit at the same time.
current urls are:
secure.mydomain.com/onlinestore/product.php?productid=12345
changing it to:
mydomain.com/product.php?productid=12345
wondering if I can do something in htaccess so if someone clicks an old link/bookmark they get redirected to the new page.
Not worried about making the dynamic part be SEO friendly just yet.
Try this. It forces a redirect with a "moved permanently" header.
RewriteEngine On
RewriteCond %{HTTP_HOST} secure\.mydomain\.com
RewriteRule onlinestore/product\.php http://mydomain.com/product.php [L,R=301,QSA]
Edit was missing some / in the RewriteCond
Edit2 Redirect wasn't going to the correct domain

Resources