Remove .htaccess 301 RedirectMatch trailing slash? - .htaccess

I have a redirectMatch rule in place:
RedirectMatch 301 /dir/subdir/safety-program(.*) /dir/subdir/safety-program/$1
When I load the safety-program address, like so:
http://localhost/dir/subdir/saftey-program/
or
http://localhost/dir/subdir/saftey-program
I get the safety-program page returned with the following address, with an extra trailing slash:
http://localhost/dir/subdir/safety-program//
Why is the match adding that second slash? How do I remove it?

the problem is here safety-program/$1 and it should be like this safety-program$1
why you should change it
the RedirectMatch comes like this
RedirectMatch [status] regex URL
So the [status] is: 301 and regex is : /dir/subdir/safety-program(.*) then there is a url which is : /dir/subdir/safety-program/$1 and this part, $1, of url will match this part ,(.*), of regex , so you put slash in this (.*) then it comes to url like this $1 including that slash , and you expressed about it like this /$1 in url , so that why double slash occurred.
That what you did exactly.
Whenever request /dir/subdir/safety-program/ it would be /dir/subdir/safety-program//
so change the line code to be :
RedirectMatch 301 /dir/subdir/safety-program(.*) /dir/subdir/safety-program$1
OR this :
RedirectMatch 301 /dir/subdir/safety-program/(.*) /dir/subdir/safety-program/$1

Related

htaccess strip query string from using redirectmatch

I'm trying to apply a redirect rule using redirectmatch 301 like below
RedirectMatch 301 ^/product-category/babies-tots/ https://mothersenvogue.com.hk/shop/all-babies-tots?
It does work. However, the appended query string like the example below does not get stripped.
http://mev-hongkong.com/product-category/babies-tots/?add_to_wishlist=8836
How can I strip the ?add_to_wishlist=8836 parameter to just redirect it to https://mothersenvogue.com.hk/shop/all-babies-tots/ ?

redirectmatch without parameters?

The url
www.ex.com/product_info.php?products_id=23&osCsid=1ed12
should be redirect to www.ex.com/pagex.htm
I've tried the .htaccess with
"RedirectMatch 301 ."star"product_info.php"star" https://ex.com/pagex.htm"
(where "star" = *)
but then the parameters are being forwarded to, so
https://ex.com/pagex.htm?products_id=23&osCsid=1ed12
How to get rid of "?" and everything on parameter stuff?
RedirectMatch directive doesn't remove query string. Use mod_rewrite based rules as this one:
RewriteEngine On
RewriteRule ^/?product_info\.php$ https://ex.com/pagex.htm? [L,NC,R=301]
Trailing ? strips off any existing query string in target URL.

ERR_TOO_MANY_REDIRECTS [duplicate]

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

htaccess redirect 301 with or without trailing slash

I have some urls looking like this:
redirect 301 /some/old/url/ http://example.com/url
redirect 301 /some/old/test/ http://example.com/test
What I do want is kind of like this:
redirect 301 /some/old/[word-match-with-or-without-trailing-slash] http://example.com/[same-word-without-trailing slash]
How do I do this? The problems:
Match both with or without slash.
Keep the match without slash for later use (see next).
Put the match witout slash at the end of the line.
Update
It seems like I could do something like this (untested):
redirect 301 /some/old/(.*) http://example.com/$1
But then how can I remove the possible trailing slash in $1?
To redirect to remove the trailing slash ,you can use :
RedirectMatch ^/some/old/(.*?)/?$ http://example.com/$1

htaccess Redirect 301 adds trailing slash ... want it removed

This is probably very basic, but I can't figure out why the heck my 301 redirect is adding a trailing slash.
I want to redirect:
http://example.com/register to https://attendee.gotowebinar.com/register/[string of numbers]
So my redirect:
Redirect 301 /register https://attendee.gotowebinar.com/register/[string of numbers]
This adds a trailing slash on the end of the redirected URL which gives me a 404.
Is there anyway I can have the 301 redirect not add the trailing slash on the end?
Don't use Redirect directive. Use RedirectMatch to get precise URL:
RedirectMatch 301 ^/register https://attendee.gotowebinar.com/register/123456
Make sure to clear your browser cache before testing this rule.

Resources