I want to redirect
mysite.com/something
to
othersite.com/someone
I am using this (in htaccess):
RedirectMatch 301 /something http://othersite.com/someone
but it redirects all pages like mysite.com/ksadkahsdhasdhkas/something and I want to redirect only mysite.com/something
How to do it?
Use regex anchors ^ and $ to make sure you don't match any unwanted URI:
RedirectMatch 301 ^/something/?$ http://othersite.com/someone
Related
I want to edit .htaccess placing the lines below into a single line:
Redirect 301 "/smartphone" "/roster.php"
Redirect 301 "/mobile" "/roster.php"
How can I do that?
The corresponding RedirectMatch directive allows you to use a regex. For example:
RedirectMatch 301 ^/(smartphone|mobile) /roster.php
Reference:
https://httpd.apache.org/docs/current/mod/mod_alias.html#redirectmatch
I have in .htaccess a redirect that should transform the url with .html in without extension which works great.
RedirectMatch 301 ^/([^/]+)/([^/]+)\.html$ /$1/$2/
I want to exclude from the rule if the filename it's named index.html how do I do that?
What I tried was
RedirectMatch 301 ^/([^/]+)/(?!index|[^/]+)\.html$ /$1/$2/
But now it isn't working at all.
You may use this regex in your rule:
RedirectMatch 301 ^/([^/]+)/(?!index)([^/]+)\.html$ /$1/$2/
Your attempts at negative lookahead doesn't seem to be correct because of | in expression.
I am trying to use a RedirectMatch to redirect all files under one directory to the main directory:
RedirectMatch 301 ^/question/.*$ https://www.sample.com/
So that if I have a URL like www.sample.com/question/what-day-is-it
It will automatically redirect to www.sample.com/what-day-is-it
The problem is when I put in the RedirectMatch like above, it redirects all URLs to the homepage: www.sample.com/question/what-day-is-it to www.sample.com
If anyone else had the same problem, a simple Redirect 301 /question / did the trick.
I want to make a redirect 301 from an old ulr to a new url.
old url: /php/zend-framework/captcha-codigo-anti-spam-zend-framework
new url: http://www.demo31.com/blog/php/zend-framework/captcha-codigo-anti-spam-zend-framework
In .htaccess I make this redirect like this ...
RedirectMatch 301 /php/zend-framework/captcha-codigo-anti-spam-zend-framework http://www.demo31.com/blog/php/zend-framework/captcha-codigo-anti-spam-zend-framework
But I've got the error "ERR_TOO_MANY_REDIRECTS".
What am I doing wrong?
It looks like both URI's are on the same host (www.demo31.com), so when you use RedirectMatch, the part of the URI that matches is part of the redirect. Example
If I go to:
http://www.demo31.com/php/zend-framework/captcha-codigo-anti-spam-zend-framework
The URI is /php/zend-framework/captcha-codigo-anti-spam-zend-framework
The RedirectMatch directive matches the URI, redirects to:
http://www.demo31.com/blog/php/zend-framework/captcha-codigo-anti-spam-zend-framework
new URI is /blog/php/zend-framework/captcha-codigo-anti-spam-zend-framework
however, the RedirectMatch directive matches the URI again since it contaings /php/zend-framework/captcha-codigo-anti-spam-zend-framework
Try changing RedirectMatch to just Redirect. Or if you only want that specific URI to redirect (as opposed to something like /php/zend-framework/captcha-codigo-anti-spam-zend-framework/some/other/stuff also getting redirected, add a few delimiters:
RedirectMatch 301 ^/php/zend-framework/captcha-codigo-anti-spam-zend-framework$ http://www.demo31.com/blog/php/zend-framework/captcha-codigo-anti-spam-zend-framework
(the ^ and $
RewriteRule ^bad_url$ good_url [R=301,L]
Thx ! works for me
I'm trying to 301 redirect from '/en' or '/en/' to '/en/home' using .htaccess, but any attempt I do results into a redirection loop '/en/home/home/home/home/home/home...'.Shouldn't it be as simple as Redirect 301 /en /en/home?
Redirect based rule keep matching /en in redirected URL as well. You can use RedirectMatch for this with regex support:
RedirectMatch 301 ^/(en)/?$ /$1/home
Also make sure to clear your browser cache when you test this.
You have to use the full URL, example:
redirect 301 /folder_wrong/name.html http://website.com/folder-right/name.html