htaccess redirect with ( sign - .htaccess

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.

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+)/?$.

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; How to copy value?

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.

.htaccess redirect path with or without trailing slash

Given the following path:
http://www.testsite.com/some/path/
I want to redirect it to
http://www.testing.com/new/stuff/
How can I make .htaccess execute that redirect regardless of whether the path has a trailing slash or not?
Edit
I've tried this:
RedirectMatch 301 /some/path(.*) /new/stuff/$1
However, it results in the following redirect
http://www.testing.com/new/stuff/?/some/path
how can I make redirect from one path to another without appending anything to the new path. I just want it to go to '/new/stuff/' with nothing on the end regardless of what, if anything, comes after '/some/path'
Just remove the $1 from your RedirectMatch's target. You should also include the ^ for the beginning of the URI:
RedirectMatch 301 ^/some/path(.*) /new/stuff/

Resources