Redirect urls with trailing slash - .htaccess

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.

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).

htaccess - Redirect all URLs containing /THIS/something/ANDTHAT/

My CMS is running into a loop often, because impossible URLs are generated. Those URLs have the format
www.xy.com/STRING1/something/STRING2/something
or
www.xy.com/something/STRING1/somethingother/anything/STRING2/something
I want to deny or block all URLs that contain STRING1 and STRING2 as segments.
Thanks for any idea!
There are many issues with the accepted answer above, so I will post a correction...
RewriteEngine on
RewriteRule ^/?STRING1/.*/STRING2/ https://example.com/ [R=301]
Note: the L flag does not make any sense when you implement an external redirection. Also the matching is not really precise in the accepted answer...
In case you want to return an explicit http status (like 403) you can also do that:
RewriteEngine on
RewriteRule ^/?STRING1/.*/STRING2/ - [R=403]
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...
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).
You can do it with this rewriting:
RewriteRule STRING1\/.*\/STRING2.* https://www.xy.com/ [R=301,L]

301 redirect using only part of original url

I am trying to 301 redirect some url from my old site structure to my new simpler site structure.
My original urls are formatted: /my-place-name-33/ for example, which I need to redirect to just /my-place-name/ completely omitting the -33
I tired adapting the solution from this thread: htaccess - 301 redirect removing part of url but my separator isn't strong enough (if that makes sense). I only have a hyphen to separate, but in most cases on the site, the my-place-name already contains hyphens.
I'm pulling my hair out trying to get around this. Any help would be amazing.
Many thanks
D
I'd say that this simple approch should do what you are looking for:
RewriteEngine on
RewriteRule ^/?(.+)-\d+/?$ /$1/ [END]
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).

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 rule creates loop

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).

Resources