RedirectMatch 301 /out/link http://www.link.com/?getparam1=something&getparam2=somethingelse&getparam3=more
When you click on mysite.com/out/link it goes to:
http://www.link.com/?getparam1=something/out/linkgetparam2=somethingelse/out/linkgetparam3=more
Basically its replacing the & sign, for the http 'get' params, with the /out/link part
Possibly try with the B flag
Escape non-alphanumeric characters before applying the transformation.
So you would have
RedirectMatch 301 /out/link http://www.link.com/?getparam1=something&getparam2=somethingelse&getparam3=more [B]
Just in case you absolutely want to avoid escaping all the ampersands &:
If you write a RewriteRule instead of a RedirectMatch directive, the ampersand in the query will not be replaced by the entire match ($0).
So, the two next lines appear same to the user: (note the second line has an unescaped &)
RedirectMatch ^/product-id/(.*) http://www.example.com/?foo=bar\&product_id=$1
RewriteRule ^/product-id/(.*) http://www.example.com/?foo=bar&product_id=$1 [R=301]
If you want the right-side URI visible to the user, use the [R] flag as indicated. If you ommit the flag, the user will be shown the left-hand URI (typically the more elaborate one, so maybe you dont need the flag).
It will act the same way, but it is likely to be less performant (google RedirectMatch vs. RewriteRule) for more on performance. Please keep in mind that all these Regex-Rules are evaluated for every single request on your webserver.
Related
My URLs are like this:
www.example.com/1234-title-of-an-event/whatever/
I need to take everything after the "1234-" (but it could be a 3 digit number as well, like "123-") and before the slash "/whatever" in order to redirect as:
www.example.com/title-of-an-event/
I am trying with the following rule (as the very last rule), but it doesn't seem to work and I only get a 500 Internal Server Error .
RewriteRule ^\/([0-9]{3,4})-(.*)\/(.*) https://www.example.com/$2 [R=301, L]
RewriteRule ^\/([0-9]{3,4})-(.*)\/(.*) https://www.example.com/$2 [R=301, L]
The 500 error is most probably caused by the space in the flags argument. It should be [R=301,L] (no space). However, this directive won't do anything in .htaccess, because of the slash prefix on the RewriteRule pattern. In a directory context (ie. .htaccess), the URL-path that is matched by the RewriteRule pattern excludes the directory-prefix, which notably ends in a slash, so the URL-path that is matched never starts with a slash. (You would need to match the slash prefix in a server/vhost context.)
This also should probably not be the "very last rule" if you have other mod_rewrite directives - there could be a conflict (without seeing your entire file).
There is no need to escape slashes in the the RewriteRule pattern. Slashes carry no special meaning here, since spaces are effectively used as regex delimiters in Apache config files.
I would also modify your regex to match everything except a slash (ie. [^/]+), instead of everything, since your regex (capturing subpattern) would match title-of-an-event/whatever in your example URL, not title-of-an-event, as intended. Since regex is greedy by default.
So, try the following instead, near the top of your .htaccess file:
RewriteRule ^\d{3,4}-([^/]+) /$1 [R=302,L]
This matches /1234-title-of-an-event and discards everything else, returning title-of-an-event in the $1 backreference. (Is the trailing /<whatever>/ required in order to make a successful match?)
\d is simply shorthand for [0-9].
There is no need to have capturing sub-patterns in the regex if the backreferences are not being used.
There is no need to include the absolute URL in the substitution unless you have multiple domains or are canonicalising the scheme/hostname in the redirected response.
Note that this is a 302 (temporary) redirect - only change to a 301 (permanent) redirect - if that is the intention - once you have confirmed that this works OK, to avoid caching issues. You will need to ensure that your browser cache is cleared before testing.
Google Search Console is showing 404 Page Not Found error for
https://example.com/page/https://example.com/page/
and the link is coming from an external website.
I want to redirect with .htaccess:
https://example.com/page/https://example.com/page/
to
https://example.com/page/
Can anyone can help me in this regard?
Try the following mod_rewrite directives at the top of your .htaccess file:
RewriteEngine On
RewriteRule ^(.*?)https?:/ /$1 [R=301,L]
This just removes any trailing part on the URL-path that starts http:/ (or https:/).
UPDATE: The ? in the capturing subpattern (.*?) makes it non-greedy, so it only captures up to the first occurrence of https:/ and discards the rest, rather than up to the last occurrence (greedy) and looping (redirect loop) until all occurrences of https:/ were removed.
Additional notes:
First test with 302 (temporary) redirect to make sure it works. Only change to 301 when confirmed, to avoid caching issues.
The URL-path that is matched by the RewriteRule pattern has already had sequences of slashes reduced to single slashes, so you can't match // (double slash) here (but I don't think you need to).
If there are query strings involved then you may need a slightly different approach and another directive, since the query string itself (as opposed to the URL-path) might contain the "repeated URL" that needs to be removed (we would need to see an example first). The RewriteRule pattern matches against the URL-path only, not the query string.
On Windows: If the (scheme and) colon (:) appears in the first path segment (ie. the malformed link is for the document root) then Apache will generate a 403 Forbidden before .htaccess is able to redirect. There is nothing you can do to avoid this since it is a limitation of the OS (colons are not allowed in filesystem paths - the 403 occurs when Apache tries to map the URL to a filesystem path). This does not happen on Linux. For example: https://example.com/https://example.com/.
UPDATE: If you are not seeing a redirect, just a 404 then you may need to enable additional pathname information (PATH_INFO) on your URLs. For example, at the top of your .htaccess file:
AcceptPathInfo On
I've the following in .htaccess:
RedirectMatch 301 /Search/[my_value]/m(.*) /Search?city=[my_value]
[my_value] is always the same in both URLs. How can I also copy [my_value] from the first URL to the other URL?
Just use the placeholder reference as documented in the alias module:
RedirectMatch 301 ^/?Search/([^/]+)/m(.*)$ /Search?city=$1
Or you can instead use a simple rewriting rule as offered by the rewriting module:
RewriteEngine on
RewriteRule ^/?Search/([^/]+)/m(.*)$ /Search?city=$1 [R=301]
Both approaches can be used in the http servers host configuration, or, if really required, in dynamic configuration files (.htaccess style files). You should however definitely prefer the first option.
You really should start to read the documentation of the tools you use. Your question is answered in there:
Alias module: https://httpd.apache.org/docs/current/mod/mod_alias.html#redirectmatch
Rewrite Module: http://httpd.apache.org/docs/current/mod/mod_rewrite.html#rewriterule
RedirectMatch uses Regular Expressions
This directive is equivalent to Redirect, but makes use of regular expressions, instead of simple prefix matching.
The supplied regular expression is matched against the URL-path, and if it matches, the server will substitute any parenthesized matches into the given string and use it as a filename.
This means, to capture part of the request, you must put it in parenthesis (...), and then use this as $1, $2, etc. in the target URL.
As #arkascha already has shown, to use [my_value], you capture it with (.+?) and add it in the target as city=$1. The second part m(.*)$ could be used as $2. But if you don't need it in the target, you can just remove the parenthesis and say m.*$, e.g.
RedirectMatch ^/Search/(.+?)/m.*$ /Search?city=$1
When everything works as it should, you may change the status code to 301. Never test with 301.
I am trying to write an htaccess rewrite rule. But it is not redirecting,
This is my present rule
RewriteEngine On
RewriteRule ^([a-zA-Z0-9]+)$ question.php?qkey=$1
that will show a url like sitename/questionkey and redirect it perfectly.
Now Iam trying to show a url like sitename/questioncatagory/questiontititle
Iam trying to use the following rule, but it is not working
RewriteEngine On
RewriteRule ^([a-zA-Z0-9]+)/^([a-zA-Z0-9]+)/^([a-zA-Z0-9]+)$ question.php?qkey=$1
First, it's probably better for clarity and maintenance to replace your ([a-zA-Z0-9]) with ([\w]+)
Secondly, your new rule doesn't work because of the caret ^ character. In the beginning, it indicates 'match beginning of the line', which surely doesn't apply 3 times total in the regex. Remove the two later ^ carets (and then make use of your additional captured groups somewhere with $1, $2, et c).
Lastly, you probably don't need to match the end of the line with the $ character. This is unfriendly to many URLs, for example ones with a trailing slash.
RewriteEngine On
RewriteRule ^([\w]+)/([\w]+)/([\w]+) question.php?qkey=$1&cat=$2&qtitle=$3
I have a similar situation to the one described in the title.
All that I need to do is map all requests in the form /search/?q=test to /test. This is because we are changing the way our search works to make it user friendly, but still want to allow for backward compatability (i.e. anyone that may have these links bookmarked etc).
However, thus far I have this:
RedirectMatch 301 /search/?q=(.*) /$1
And that doesn't work, but:
RedirectMatch 301 /search/(.*) /$1
does...
Any idea why?
Cheers.
have you tried with RedirectMatch 301 /search/\?q=(.*) /$1 ?
I am using the \ before the ? (to force it being used as a literal character) in case it gets treated as a special char (which it is, in regular expressions..)