htaccess redirection, urls with hashes - .htaccess

I have urls like these:
http://domain.com/test/MzA5
http://domain.com/test/AtbC
http://domain.com/test/4gCA
How to make htaccess rule which will redirect all urls like http://domain.com/test/[hash here] to one page, for example http://domain.com/page.html

Try:
Redirect 301 /test/MzA5 /page.html
If you have a lot of those hashes, you can make a lot of 301 redirects:
Redirect 301 /test/MzA5 /page.html
Redirect 301 /test/AtbC /page.html
Redirect 301 /test/4gCA /page.html
or consider using a RewriteMap (which requires access to the server config, won't work in .htaccess file)

To redirect all URLs of the form /test/*/ to page.html, use the following rewrite:
RewriteEngine On
RewriteRule ^/test/[^/]+/? /page.html [R=301, L]

Related

How to make a 301 redirect of several get requests

I've finished a website, now i'm changing old website to new and creating 301 redirects in .htaccess.
I have a sitemap containing aroud 100 equal get requests like:
?foodmenu=name-of-the-dish
In the new website there is no similar request and all this requests must be redirected to a fixed website, lets say http://myrestaurant.com/menu
There is a way I can avoid 100+ lines of almost similar httacces lines:
Instead of:
Redirect 301 ?foodmenu=name-of-the-dish1 http://myrestaurant.com/menu
Redirect 301 ?foodmenu=name-of-the-dish2 http://myrestaurant.com/menu
Redirect 301 ?foodmenu=name-of-the-dish3 http://myrestaurant.com/menu
Redirect 301 ?foodmenu=name-of-the-dish4 http://myrestaurant.com/menu
Make all this in one line or function, something similar to:
Redirect 301 ?foodmenu* http://myrestaurant.com/menu
NOTE: this requests are multilanguage, so I will need more than one rule / redirect:
Redirect 301 ?foodmenu=name-of-the-dish3 http://myrestaurant.com/menu
Redirect 301 /ca?foodmenu=name-of-the-dish3 http://myrestaurant.com/ca/menu
Redirect 301 /es?foodmenu=name-of-the-dish3 http://myrestaurant.com/es/menu
You can not match against query strings in redirect directive, try :
RewriteEngine on
RewriteCond %{THE_REQUEST} \?foodmenu=([^\s]+) [NC]
RewriteRule ^ %{REQUEST_URI}menu? [L,R]

Redirect www.domain.com/112 to www.domain.com/ using htaccess 301 redirect

I have thousands of URLs on my website like:
www.domain.com/112
and
www.domain.com/113
I'd like to redirect these straight to www.domain.com.
Can someone explain how I do this using the 301 Redirect with htaccess?
Thank you
RewriteEngine on
RewriteRule ^(\d+)/?$ / [R=301,L]
This will redirect any digit-based request in document root to index page

How to do 301 redirects with folders & subfolders?

I need to redirect all the old URLs on my site to new URLs, but am having issues. A given URL and its subfolders need redirecting to the same page.
For example, this is the first one:
redirect 301 /panache/sports-bra/ http://www.newdomain.co.uk/sports-bra.html
This works ok. However there are these size sub-pages which need to redirect to the same location:
redirect 301 /panache/sports-bra/30DD http://www.newdomain.co.uk/sports-bra.html
redirect 301 /panache/sports-bra/30E http://www.newdomain.co.uk/sports-bra.html
redirect 301 /panache/sports-bra/30F http://www.newdomain.co.uk/sports-bra.html
And these don't work, I end up at a location like the following:
http://www.newdomain.co.uk/sports-bra.html30DD
See the way the last part of the path is appended to the url? I'm guessing it's because the second redirect is conflicting with the initial 301 redirect?
I have also tried using this rewrite rule but have had no luck. The site is Magento so I don't know if this has some effect? mod_rewrite is enabled on the server.
RewriteRule ^panache/sports-bra/ http://www.newdomain.co.uk/sports-bra.html [R=301]
Any help mucha ppreciated.
Try this.
RewriteEngine on
RewriteRule ^(panache/sports-bra/.*)$ /sports-bra.html [L,R=301,NC]

301 redirect from htaccess

I'm trying to create permanent redirects for outdated URL's on my site.
For example I have: www.mydomain.com/?v=tv and want it have a permanent 301 redirect to www.mydomain.com/tv.php
I tried this code in my htaccess file but it did not work:
Redirect /?v=tv http://mydomain.com/tv.php
Any Help?
RewriteRule ^tv.php$ http://guessthelogo.com/?v=tv [R=301,L]
I think your missing "301" after "Redirect":
Redirect 301 /?v=tv http://guessthelogo.com/tv.php

301 redirect any page to one specific page

IS there a way with just htaccess 301 redirect to redirect any page on a domain to a specific page on another domain.
eg. I was domain.com/index.html and domain.com/contact.html to both redirect to newsite.com/index.html
But I am wanting to do this without having to list each of the pages specifically.
can my 301 redirect be just something like
301 * http://newsite.com/index.html
or how should it be set up. Unfortunately I don't have access to mod rewrite so I cant use mod rewrite to make it work.
Had an issue similar to this using wordpress and trying to remove all .asp extensions from pages, this worked pasted at the top of my .htaccess file
## 301 Redirects
RewriteCond %{QUERY_STRING} ^$
RewriteRule ^(.*)\.asp$ $1? [R=301,NE,NC,L]
Yes, that is possible -- instead of mod_rewrite you need to use mod_alias (which has more chances to be enabled).
This one will redirect everything to index.html on newsite.com/
RedirectMatch 301 ^/(.*)$ http://newsite.com/index.html
This one will redirect everything to the same path but on another domain: (e.g. oldsite.com/meow.php => newsite.com/meow.php)
RedirectMatch 301 ^/(.*)$ http://newsite.com/$1

Resources