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.
Related
I have a URL that I need to add a segment /#!/ to.
This isn't working because I get a loop.
Options +FollowSymLinks
RewriteEngine On
RedirectMatch "/constantsegment/(.*)" "/constantsegment/#!/$1"
For example, let's say my URL is http://example.com/constantsegment/changeablesegment. I need the site to redirect to http://example.com/constantsegment/#!/changeablesegment.
Notice that there is a URL segment(folder) called #! in between constantsegment and changeablesegment.
The problem I am running into is that a redirect loop is created when trying to append /#!/ to the /constantsegment/. How can I just added /#!/ to the end and then add all the other segment(s) after that.
Again
http://example.com/constantsegment/changeablesegment
Should redirect to
http://example.com/constantsegment/#!/changeablesegment
Another example (in this case products is the constant segment)
http://example.com/products/cars/blue
Should redirect to
http://example.com/products/#!/cars/blue
RedirectMatch "/constantsegment/(.*)" "/constantsegment/#!/$1"
Simply change .* to .+ (1 or more) to avoid matching the target URL and causing a redirect loop. Because the fragment identifier is not passed back to the server on the redirected request.
You can avoid repetition by capturing the first "constant" path segment as well. For example:
RedirectMatch "/(constantsegment)/(.+)" "/$1/#!/$2"
Note that the above mod_alias directive (ie. RedirectMatch) has nothing to do with mod_rewrite (ie. RewriteEngine On). And neither is Options FollowSymLinks required here. Unless you are using mod_rewrite elsewhere in your .htaccess file.
If you are already using mod_rewrite for other redirects then you should probably use mod_rewrite (ie. RewriteRule) instead (to avoid unexpected conflicts). For example:
Options FollowSymLinks
RewriteEngine On
RewriteRule ^(constantsegment)/(.+) /$1/#!/$1 [NE,R,L]
The NE flag is required to prevent the # (hash) being URL encoded and considered part of the URL-path.
Note that all these redirects are 302 (temporary).
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 need to make a .htaccess 301 redirect to get the next results.
Original URL
example.com/en/pageX.html
example.com/fr/pageX.html
Final URL
example.com/en/pageX.html
example.com/fr/pageX.html
At the same time:
Original URL
example.com/en/X/pageX.html
example.com/fr/X/pageX.html
Final URL
example.com/en/pageX.html
example.com/fr/pageX.html
And also at the same time:
Original URL
example.com/en/X/X/pageX.html
example.com/fr/X/X/pageX.html
Final URL
example.com/en/pageX.html
example.com/fr/pageX.html
And also at the same time:
Original URL
example.com/en/X/X/X/pageX.html
example.com/fr/X/X/X/pageX.html
Final URL
example.com/en/pageX.html
example.com/fr/pageX.html
For example and summarizing:
Original URL
example.com/en/food.html
example..com/fr/nourriture.html
example..com/en/food/fruits.html
example.com/fr/nourriture/fruits.html
example.com/en/food/fruits/apples.html
example.com/fr/nourriture/fruits/pommes.html
example.com/en/food/fruits/apples/apple_golden.html
example.com/fr/nourriture/fruits/pommes/pomme_golden.html
Final URL
example.com/en/food.html
example.com/fr/nourriture.html
example.com/en/fruits.html
example.com/fr/fruits.html
example.com/en/apples.html
example.com/fr/pommes.html
example.com/en/apple_golden.html
example.com/fr/pomme_golden.html
Thank you for your help.
Josep
You can use mod_rewrite to remove all folders between the language identifier the actual page name and redirect the browser to show the new url. See the documentation for more information.
RewriteEngine on
RewriteRule ^(en|fr)/.*?([^/]+\.html)$ $1/$2 [R,L]
This rule matches the language tag at the beginning of the path with ^(en|fr) and puts it in capture group 1. We then match a slash and as little characters as possible (/.*?). Finally it matches the page name with ([^/]+\.html)$ and puts it in capture group 2. Finally we put the url back together by using group substitution ($1 and $2) and forcing a temporary redirect with the [R] flag. When the rule works as expected, you can change the redirect flag to [R=301] to make it a permanent redirect. Permanent redirects are cached by the browser, preventing useless requests to the server and forcing sites like google to index it as if the two urls are the same page.
I am trying to redirect a subfolder as well as anything after it to the home page.
For example:
example.com/subfolder/extra-stuff > example.com
The extra-stuff is constantly changing and auto generated, so I want the redirect to remove that as well.
I am using:
Redirect 301 /subfolder(.*) http://www.example.com
However, this will result in http://www.example.com/extra-stuff.
Is there a way I can say if /subfolder(and anything else after subfolder) redirect to home?
Thanks for any suggestions!
The Redirect directive uses simple prefix-matching and everything after the match is copied onto the end of the target URL (which is what you are seeing here). However, the Redirect directive also does not support regex syntax, so a "pattern" like (.*) on the end will actually match the literal characters (, ., * and ) - which shouldn't have worked in your example?!
You'll need to use RedirectMatch instead (also part of mod_alias), which does use regex, and is not prefix matching.
For example:
RedirectMatch 301 ^/subfolder http://www.example.com/
Any request that starts /subfolder will be redirected to http://www.example.com/ exactly.
You'll need to clear your browser catch before testing.
You tagged your question "Magento" (which is probably using mod_rewrite). You should note, however, if you are already using mod_rewrite for rewrites/redirects then you should probably be using mod_rewrite instead of mod_alias to do this redirect, since you can potentially get conflicts.
For example, the equivalent mod_rewrite directive would be:
RewriteRule ^subfolder http://www.example.com/ [R=301,L]
Note there is no slash prefix on the RewriteRule pattern. This would need to go near the top of your .htaccess file.
I'm trying to find a redirect that will remove part of a query string from a URL, but leave the remaining query string.
Now I can do this fine via a single URL redirect, but there are hundreds of these URLs. So I'm trying to find a rule that might be able to do this for all of them in one fell swoop, so I don't have to make one for each and any new ones will get redirected automatically.
I'm trying to remove start=0& from the URLs, here are some examples:
www.example.com/products.php?start=0&category=Pens%20Ballpens
redirects too:
www.example.com/products.php?category=Pens%20Ballpens
and
www.example.com/products.php?start=0&category=Jackets
redirects too:
www.example.com/products.php?category=Jackets
Try adding this to your htaccess in the document root:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^(.*)&?start=0(.*)$
RewriteRule ^/?products\.php$ /products.php?%1%2 [R=301,NE]