IIS redirect with and without trailing slash - iis

I have made a rewrite map and what the entries to match both the target address with or without the trailing slash.
Example:
/about-us/page
/about-us/page/
I have tried with adding a trailing slash in the match like:
{redirects:{REQUEST_URI}}
{redirects:{REQUEST_URI}/}
And matching any, but it doesn't seem to work.
Any ideas?

Related

htaccess redirect with GET values

i'm trying to redirect from one url to another. First url has GET values (/product/category/name/something-123/?a=1&b=2), which should be omitted.
After redirection I have all get values in new URL (https://example.com/category/asdf.html/?a=1&b=2) - how can I skip all get values during redirection?
My code:
Redirect 301 /product/category/name/something-123 https://example.com/category/asdf.html
To remove the query string entirely, you'll need to use mod_rewrite (RewriteRule) instead of mod_alias (Redirect). For example:
RewriteEngine On
RewriteRule ^product/category/name/something-123/?$ https://example.com/category/asdf.html [QSD,R=301,L]
The QSD flag discards the original query string (requires Apache 2.4).
Aside: Note that you had omitted the trailing slash on your source URL (in the Redirect directive), which is what caused the additional slash on the redirected URL when requesting the URL with a trailing slash (as in your example). The Redirect directive is prefix-matching and everything after the match is copied onto the end of the target URL.
In the above example I've made the trailing slash optional on the requested URL.
Note that you will need to clear your browser cache before testing. Test first with 302s to avoid potential caching issues.

Redirection from after slash word to query

I am very new in coding and redirection in .htaccess.
I would need a redirection for a lot of URLs with the slug /brand/.
For example:
https://example.com/brand/AAA to /shop/?filter_marke=AAA
https://example.com/brand/BBB to /shop/?filter_marke=BBB
https://example.com/brand/CCC to /shop/?filter_marke=CCC
and so on.
You could perform the "redirect" like the following using mod_rewrite:
RewriteEngine On
RewriteRule ^brand/(\w+)$ /shop/?filter_marke=$1 [R=302,L]
The order of rules can be important. This would need to go near the top of the .htaccess file, before any existing rewrites. If this is a WordPress site, then it would need to go before the # BEGIN WordPress comment marker.
The $1 backreference in the substitution string (2nd argument) contains the value of the word after /brand/ in the URL-path.
UPDATE:
I only forgot to mention that there is a slash after the variable, means the incoming link looks like ..../AAA/
In that case you can simply append a trailing slash to the end of the pattern, ie. ^brand/(\w+)/$. Or make the trailing slash optional so it matches both. ie. ^brand/(\w+)/?$.

Rewriterule after 3 or 4 characters

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.

How to redirect double URL to single URL with htaccess

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

htaccess redirect with ( sign

I have this link
/Kurs-og-konferanser/Inspirasjon-og-motivasjon/Kvinner-i-tiden/Talere-i-Oslo-31.-mai/(event)/1247843
Which I want to redirect to
alle-kurs-og-konferanser/kvinner-i-tiden
So add this to my .htaccess
Redirect 301 /Kurs-og-konferanser/Inspirasjon-og-motivasjon/Kvinner-i-tiden/Talere-i-Oslo-31.-mai/(event)/1247843 http://{url}/alle-kurs-og-konferanser/kvinner-i-tiden
If I just add this to my .htaccess file it redirects to
alle-kurs-og-konferanser/kvinner-i-tiden/Talere-i-Oslo-31.-mai/(event)/1247843
So my guess its something with (event) so how do I escape this - I have already tried with (event) but this just add some extra slashes.
See the documentation for Redirect:
Then any request beginning with URL-Path will return a redirect request to the client at the location of the target URL. Additional path information beyond the matched URL-Path will be appended to the target URL.
If you don't want the additional path information, don't use Redirect. Use RedirectMatch instead. You will need to express the URI you are matching against as a regular expression, this includes anchoring it to the start of the string (^) and escaping any characters that are regular expression metacharacters.

Resources