Redirect rule creates loop - .htaccess

I have a redirect rule on htaccess that simply points a parent page to its child page.
Redirect 301 /swu-members-only/ /swu-members-only/home/
However, when I try to access the page i'm trying to redirect, it returns a URL like this: http://simonwu.com.au/swu-members-only/home/home/home/home/home/home/home/home/home/home/home/home/home/home/home/home/home/home/home/home/home
What causes the redirect loop like this to happen? Any suggestions to fix is greatly appreciated.

The reason for the redirection loop is that the pattern you rewrite also matches your target path. So when the client sends the next request to the specified target it again gets redirected...
You can get around that by forcing an exact match:
RedirectMatch "^/swu-members-only/?$" "/swu-members-only/home/"
Another option is to use the rewriting module which offers much more flexibility:
RewriteEngine on
RewriteRule "^/?swu-members-only/?$" "/swu-members-only/home/" [R=302]
And a general remark: you should always prefer to place such rules in the http servers host configuration instead of using dynamic configuration files (".htaccess"). Those dynamic configuration files add complexity, are often a cause of unexpected behavior, hard to debug, they really slow down the http server, often for nothing and they can open pathways to security nightmares. They are only provided for situations where you do not have access to the real http servers host configuration (read: really cheap service providers).

Related

Having trouble redirecting URL when query string is present in .htaccess

I updated my website, and I'm attempting to make sure a bunch of old URLs redirect to the correct page. Some of my links are working fine, while others aren't.
Example:
Redirect 301 /compare-suites/ /en/our-products/compare-suites
Redirect 301 /compare-suites/?lang=fr /fr/nos-produits/comparaison-des-suites/
The first redirect works perfectly, but the second one redirects to /en/our-products/compare-suites?lang=fr, ignoring my second rule.
The alias module which offers the Redirect directive is unable to handle the query string inside a URL. That is clearly stated in the documentation. Along with the hint to use the rewriting module instead.
I strongly recommend that you start reading the documentation of the utilities you use:
Alias module: https://httpd.apache.org/docs/current/mod/mod_alias.html
Rewrite Module: http://httpd.apache.org/docs/current/mod/mod_rewrite.html
As typical for OpenSource they are of excellent quality and come with great examples.
This would be the approach you are probably looking for:
RewriteEngine on
RewriteCond %{QUERY_STRING} (^|&)lang=fr(&|$)
RewriteRule ^/?compare-suites/?$ /fr/nos-produits/comparaison-des-suites/ [QSD,R=301,END]
RewriteRule ^/?compare-suites/?$ /en/our-products/compare-suites [R=301,END]
Note that the more specialized exception has to be implemented before the more general fallback, so further up in the configuration file.
It is a good idea to start out with a 302 temporary redirection and only change that to a 301 permanent redirection later, once you are certain everything is correctly set up. That prevents caching issues while trying things out...
In case you receive an internal server error (http status 500) using the rule above then chances are that you operate a very old version of the apache http server. You will see a definite hint to an unsupported [END] flag in your http servers error log file in that case. You can either try to upgrade or use the older [L] flag, it probably will work the same in this situation, though that depends a bit on your setup.
This implementation will work likewise in the http servers host configuration or inside a distributed configuration file (".htaccess" file). Obviously the rewriting module needs to be loaded inside the http server and enabled in the http host. In case you use a distributed configuration file you need to take care that it's interpretation is enabled at all in the host configuration and that it is located in the host's DOCUMENT_ROOT folder.
And a general remark: you should always prefer to place such rules in the http servers host configuration instead of using distributed configuration files (".htaccess"). Those distributed configuration files add complexity, are often a cause of unexpected behavior, hard to debug and they really slow down the http server. They are only provided as a last option for situations where you do not have access to the real http servers host configuration (read: really cheap service providers) or for applications insisting on writing their own rules (which is an obvious security nightmare).

Redirect only parent page with subpages

I want to redirect one of the main pages of mysite with its sub pages.
My current URL;
www.mysite.com/article/service-1
www.mysite.com/article/service-2
I want to redirect to;
www.mysite.com/publication/service-1
www.mysite.com/publication/service-2
How can I redirect so?
Your question is a big vague as to what you actually try to achieve. So I will give you two answers as a starting point:
I assume you actually want an internal rewrite, not a rediretion. If so this will do:
RewriteEngine on
RewriteRule ^/?article/(.*)$ /publication/$1 [END,QSA]
In case you receive an internal server error (http status 500) using the rule above then chances are that you operate a very old version of the apache http server. You will see a definite hint to an unsupported [END] flag in your http servers error log file in that case. You can either try to upgrade or use the older [L] flag, it probably will work the same in this situation, though that depends a bit on your setup.
If you really want a redirection as you write in your question, then this variant will do, it will actually redirect the client, so change the visible URL in the browser:
RewriteEngine on
RewriteRule ^/?article/(.*)$ /publication/$1 [R=301,QSA]
It is a good idea to start out with a 302 temporary redirection and only change that to a 301 permanent redirection later, once you are certain everything is correctly set up. That prevents caching issues while trying things out...
Both rules will work likewise in the http servers host configuration or inside a dynamic configuration file (".htaccess" file). Obviously the rewriting module needs to be loaded inside the http server and enabled in the http host. In case you use a dynamic configuration file you need to take care that it's interpretation is enabled at all in the host configuration and that it is located in the host's DOCUMENT_ROOT folder.
And a general remark: you should always prefer to place such rules in the http servers host configuration instead of using dynamic configuration files (".htaccess"). Those dynamic configuration files add complexity, are often a cause of unexpected behavior, hard to debug and they really slow down the http server. They are only provided as a last option for situations where you do not have access to the real http servers host configuration (read: really cheap service providers) or for applications insisting on writing their own rules (which is an obvious security nightmare).

Redirect urls with trailing slash

My website currently online is completely static and all the URLs have a trailing slash at the end : https://www.website.com/blog/article-1/
I'm working on my new website which is using Prestashop. On Prestashop, URLs don't have a trailing slash : https://www.website.com/blog/article-1
Problem: I have an excellent SEO on my current website and I need to keep the actual URLs (with trailing slash) available. For user experience, I'd like URLs to work with or without trailing slash.
How can I redirect my new URLs to the same URL + trailing slash? If possible, I'd like to rewrite URLs so that users always see the URL with a trailing slash.
Example :
https://www.website.com/blog/article-1/ is redirected to https://www.website.com/blog/article-1 and the URL visible in the address bar is https://www.website.com/blog/article-1/.
Well, ask "How can I redirect my new URLs to the same URL + trailing slash"...
The answer obviously is: by implementing exactly that rule. There are thousands of examples for this alone here on SO. None of those helped? Why not?
Anyway, here is another one:
RewriteEngine on
RewriteRule ^/blog/([^/]+)$ /blog/$1/ [R=301]
RewriteRule ^/blog/([^/]+)/$ /blog/$1 [END]
You need to take care to send out references with leading slashes with this setup. Since otherwise your site will be dead slow, since the clients will have to request every single page twice due to the redirection then required for every single page...
It is a good idea to start out with a 302 temporary redirection and only change that to a 301 permanent redirection later, once you are certain everything is correctly set up. That prevents caching issues while trying things out...
In case you receive an internal server error (http status 500) using the rule above then chances are that you operate a very old version of the apache http server. You will see a definite hint to an unsupported [END] flag in your http servers error log file in that case. You can either try to upgrade or use the older [L] flag, it probably will work the same in this situation, though that depends a bit on your setup.
This rule will work likewise in the http servers host configuration or inside a dynamic configuration file (".htaccess" file). Obviously the rewriting module needs to be loaded inside the http server and enabled in the http host. In case you use a dynamic configuration file you need to take care that it's interpretation is enabled at all in the host configuration and that it is located in the host's DOCUMENT_ROOT folder.
And a general remark: you should always prefer to place such rules in the http servers host configuration instead of using dynamic configuration files (".htaccess"). Those dynamic configuration files add complexity, are often a cause of unexpected behavior, hard to debug and they really slow down the http server. They are only provided as a last option for situations where you do not have access to the real http servers host configuration (read: really cheap service providers) or for applications insisting on writing their own rules (which is an obvious security nightmare).
If you mean default prestashop links like products, categories etc. you can just change their way to be built. Prestsahop allows us to achieve this within admin-panel Configure->Shop Parameters->Traffic & SEO->SEO and URL's>Schema of URLs (for PS 1.7).
And there change an URL in interest, for example, Route to category
from {id}-{rewrite} to {id}-{rewrite}/. And you won't need to redirect anything.

htaccess redirect folder and subfolders to one url only

I must be the first person to be frustrated with htaccess redirects lol.
Have searched and can't find the solution to this:
I've got several 404s like this:
https://www.example.com/category/uncategorized/page/4/detox
https://www.example.com/category/uncategorized/page/5/detox
https://www.example.com/category/uncategorized/page/6/detox
https://www.example.com/category/uncategorized/page/7/detox
I want to redirect like this:
FROM
https://www.example.com/category/uncategorized/
and all subfolders or pages under this
TO
https://www.example.com/category/alcohol-drug-rehab/
(this exact url only and all subfolders should redirect to this subfolder)
I've tried
RedirectMatch 301 ^/category/uncategorized/(.*)$ https://www.example.com/category/alcohol-drug-rehab/
...and several other variations.
However this results in redirections like this:
https://www.example.com/category/alcohol-drug-rehab/page/4/detox
https://www.example.com/category/alcohol-drug-rehab/page/5/detox
https://www.example.com/category/alcohol-drug-rehab/page/6/detox
https://www.example.com/category/alcohol-drug-rehab/page/7/detox
How do I alter the redirect to stop the rewritten url showing the subfolders after, and just redirect to the page only, at https://www.example.com/category/alcohol-drug-rehab/?
Would prefer straight forward rewrite statements if possible as opposed to mod rewrites, thanks.
Thanks
Just ignore anything trailing the initial path:
RewriteEngine on
RewriteRule ^/?category/uncategorized/ https://www.example.com/category/alcohol-drug-rehab/ [R=301,QSD]
This rule will work likewise in the http servers host configuration or inside a dynamic configuration file (".htaccess" file). Obviously the rewriting module needs to be loaded inside the http server and enabled in the http host. In case you use a dynamic configuration file you need to take care that it's interpretation is enabled at all in the host configuration and that it is located in the host's DOCUMENT_ROOT folder.
And a general remark: you should always prefer to place such rules in the http servers host configuration instead of using dynamic configuration files (".htaccess"). Those dynamic configuration files add complexity, are often a cause of unexpected behavior, hard to debug and they really slow down the http server. They are only provided as a last option for situations where you do not have access to the real http servers host configuration (read: really cheap service providers) or for applications insisting on writing their own rules (which is an obvious security nightmare).

Mass redirect urls through .htaccess

I changed the path of some urls and would like to get some help if possible, to create a rule in my .htaccess to redirect them in bulk.
The old url were like this one: https://www.mastersol.gr/hliakoi-thermosifones-solar/epilektikoi/mastersol-plus-wifi/product/430-160-2-plus-wifi-mastersol
The new urls are like this one: https://www.mastersol.gr/iliakoi-thermosifones/epilektikoi/mastersol-plus-wifi/product/430-160-2-plus-wifi-mastersol
As you can see, I changed only the part /hliakoi-thermosifones-solar/ to /iliakoi-thermosifones/ and I need to 301 redirect all /hliakoi-thermosifones-solar/ urls to /iliakoi-thermosifones/.
Could you help on this?
Thanks!
The task appears to be straight forward: you implement a rule matching only those requests still using the old pattern, capture the stuff following after that token to be changed and redirect (or itnernally rewrite) to a newly assembled URL:
For an external redirection:
RewriteEngine on
RewriteRule ^/?hliakoi-thermosifones-solar(.*)/?$ https://www.mastersol.gr/hliakoi-thermosifones$1 [R=301,END]
For an internal rewrite:
RewriteEngine on
RewriteRule ^/?hliakoi-thermosifones-solar(.*)/?$ /hliakoi-thermosifones$1 [END]
If you receive back an http status 500 (internal server error) using those rules then chances are that you are using a very old version of the apache http server and have to use the [L] flag instead of the [END] flag.
And a general remark: you should always prefer to place such rules in the http servers host configuration instead of using dynamic configuration files (".htaccess"). Those dynamic configuration files add complexity, are often a cause of unexpected behavior, hard to debug, they really slow down the http server, often for nothing and they can open pathways to security nightmares. They are only provided for situations where you do not have access to the real http servers host configuration (read: really cheap service providers).

Resources