.htaccess permanent redirect - browser strangely adds GET variable to URL - .htaccess

I am trying to implement a very basic redirect for specific pages with htaccess, however the browser adds a GET variable to the new URL after the redirect:
Redirect 301 /branding/ABCDE http://example.com/branding/NEW
New URL in browser:
http://example.com/branding/NEW?slug=ABCDE
How can I correct this so that the ?slug=ABCDE does not show up?

Without seeing your .htaccess file, the best guess is that you have a conflict with other (specifically mod_rewrite) directives in your .htaccess file. There is nothing actually wrong with the Redirect directive itself that you posted.
The Redirect directive is a mod_alias directive and executes after mod_rewrite, despite the apparent order of these directives in your .htaccess file. A URL of the form /branding/ABCDE most probably requires rewriting (using mod_rewrite) for it to be useful by your application. This rewrite occurs before the Redirect, but any query string will be maintained.
It is not recommended to mix both Redirect and RewriteRule directives in the same config file, because of these unexpected conflicts.
Change your redirect to use mod_rewrite instead and make sure this is near the top of your config file. For example:
RewriteEngine On
RewriteRule ^branding/ABCDE$ /branding/NEW [QSD,R=301,L]
You will need to ensure your browser cache is cleared before testing, since the erroneous 301 (permanent) redirect will likely have been cached by your browser. To avoid caching issues, it is advisable to first test with 302 (temporary) redirects.
The QSD flag (Apache 2.4+) will discard any query string that might be present on the initial request.

Related

301 Redirect with cet= parameter

I would like to make a 301 redirect of a url in http://website.com/example-old/?cet=3132 format to a page https://www.website.com/example-new/.
I have tried several times via .htaccess with the classic method:
redirect 301 /example-old/?cet=3132 https://www.website.com/example-new/
The redirect works, but I don't get what I want. In fact the final url becomes https://www.website.com/example-new/?cet=3132.
In short, the ?cet=3132 doesn't disappear and this is not good for me.
I found this invaluable resource which recommends a mod_rewrite method: 301 redirect for old urls with language parameter
Sure I'll try it, but I was wondering: will it work even with the cet=3132 parameter?
Thanks to those who can answer, best regards.
Sure you can use the rewriting module for that:
RewriteEngine on
RewriteCond %{QUERY_STRING} ^cet=3132$
RewriteRule ^/?example-old/?$ https://www.example.com/example-new/ [QSD,L,R=301]
The QSD flag will take care to remove the query string during the redirection.
It is a good idea to start out with a R=302 temporary redirection and to only change that to a R=301 permanent redirection once everything works as intended. That prevents nasty caching issues.

HTACCESS 301 redirect keep sending to the wrong page

I am trying to redirect an old page from a website I have redesigned, to the new one, but it's not working.
Here's my 2 lines of code in the .htaccess file regarding that domain:
Redirect 301 /deaneco http://solutionsgtr.ca/fr/deaneco/accueil.html
RewriteRule ^/deaneco/contact http://solutionsgtr.ca/fr/deaneco/contact.html [R=301,L,QSA]
If go on the solutionsgtr.ca/deaneco/contact URL, it gives me the following page:
http://solutionsgtr.ca/fr/deaneco/accueil.html/contact
The first rule works though (deaneco/ to solutionsgtr.ca/fr/deaneco/accueil.html).
I feel like both lines are being mixed together and are giving me the wrong page, that doesn't exist so I get a 404 error.
There are a couple of issues here:
The Redirect directive (part of mod_alias) is prefix-matching and everything after the match is appended on the end of the target URL. This explains the redirect you are seeing.
The RewriteRule (mod_rewrite) pattern ^/deaneco/contact will never match in a .htaccess context since the URL-path that is matched does not start with a slash. So, this rule is not doing anything currently.
You should avoid mixing redirects from both modules since they execute independently and at different times during the request (mod_rewrite executes first, despite the apparent order of the directives).
Either use mod_alias, ordering the directives most specific first:
Redirect 301 /deaneco/contact http://solutionsgtr.ca/fr/deaneco/contact.html
Redirect 301 /deaneco http://solutionsgtr.ca/fr/deaneco/accueil.html
NB: You will need to clear your browser cache, since the erroneous 301 (permanent) redirect will have been cached by the browser. Test with 302 (temporary) redirects to avoid potential caching issues.
OR, if you are already using mod_rewrite for other redirects/rewrites then consider using mod_rewrite instead (to avoid potential conflicts as mentioned above):
RewriteEngine On
RewriteRule ^deaneco/contact$ http://solutionsgtr.ca/fr/deaneco/contact.html [R=301,L]
RewriteRule ^deaneco$ http://solutionsgtr.ca/fr/deaneco/accueil.html [R=301,L]
The QSA flag is not required, since the query string is passed through to the substitution by default.
The order of the RewriteRule directives are not important in this instance, since they match just that specific URL.
If go on the solutionsgtr.ca/deaneco/contact URL
If you are redirecting to the same host then you don't need to explicitly include the scheme + hostname in the target URL, since this will default.

Why my rewrite rule in htaccess is working in some case only?

I try to rewrite some of my URLs with a .htaccess file but it didn't work as expected.
This is the rewrite rule in my .htaccess file :
RewriteRule ^(index|administration)/([A-Za-z0-9-]+)(\.php)?$ index.php?c=$1&t=$2 [QSA]
When I go on www.example.com/index/main, I get a 404 error code.
So I try to change my rewrite rule to
RewriteRule ^index.php$ index.php?c=index&t=main [QSA]
Then I go to www.example.com/index.php and the webpage displays perfectly with all the datas in $_GET (c = index and t = main).
So I don't know why my first rule is not working. Let me see if you have any idea.
Is it possible that my server wants to enter the index folder, then the main folder for my first rule without taking care of my .htaccess (www.example.com/index/main) ?
You need to ensure that MultiViews (part of mod_negotiation) is disabled for this to work correctly. So, add the following at top of your .htaccess file:
Options -MultiViews
If MultiViews is enabled (it's disabled by default, but some hosts do sometimes enable this in the server config) then when you request /index/main where /index.php already exists as a physical file then mod_negotiation will make an internal request for index.php before mod_rewrite is able to process the request. (If index.html also exists, then this might be found first.)
(MultiViews essentially enables extensionless URLs by mocking up type maps and searching for files in the directory - with the same basename - that would return a response with an appropriate mime-type.)
If this happens then your mod-rewrite directive is essentially ignored (the pattern does not match, since it would need to check for index.php) and index.php is called without the URL parameters that your mod_rewrite directive would otherwise append.
it perfectly works by disabling the MultiViews Option in my .htaccess
This would ordinarily imply its your script (ie. index.php) that is triggering the 404 (perhaps due to missing URL parameters?), rather than Apache itself?
However, if you were seeing an Apache generated 404 then it would suggest either:
You also have an index.html file, which is found before index.php. .html files do not ordinarily accept path-info (ie. /main) so would trigger a 404.
OR, AcceptPathInfo Off is explicitly set elsewhere in the config, which would trigger a 404 when the request is internally rewritten to /index.php/main (by mod_negotiation).

htaccess Redirect 301 is chaining itself

I have around 100+- redirects in a .htaccess file and some urls are chaining each other.
For example:
Redirect 301 /air-india-trial/air-india-promo-conditions/ /features/
Goes to
/trial/air-india-promo-conditions/
Because of
Redirect 301 /air-india-trial/ /trial/
So the URL is replacing '/air-india-trial/' for '/trial/' because of the 2nd redirect being called. I already tried to put https://example.com/ before the 2nd URL in the Redirect 301 but that doesn't work. Not sure if it matters but the URL /air-india-trial/ doesn't exist on the new website. The domain is the same as the old website tho. Anyone that has an idea to fix those redirect chains?
Redirect 301 /air-india-trial/air-india-promo-conditions/ /features/
Goes to
/trial/air-india-promo-conditions/
Presumably you mean a request for /air-india-trial/air-india-promo-conditions/ ends up being redirected to /trial/air-india-promo-conditions/ (that directive doesn't "go to" anything).
...because of the 2nd redirect being called. I already tried to put https://example.com/ before the 2nd url in the Redirect 301 but that doesn't work.
You can't put https://example.com/ as part of the URL in the first argument - if that is what you are referring to? It simply won't match. It matches against the URL-path only.
Since the Redirect directive is prefix-matching (and everything after the match is copied onto the end of the target URL), you need to order your Redirect directives in order of specificity. The most specific (ie. longest path) redirect needs to be before the less specific redirects.
mod_alias Redirect directives do not "chain" together as you seem to imply.
So, in your example, the following should work to redirect /air-india-trial/air-india-promo-conditions/ to /features/:
Redirect 301 /air-india-trial/air-india-promo-conditions/ /features/
Redirect 301 /air-india-trial/ /trial/
(Although you do perhaps suggest that the directives are already in order (or are you just referring to the order in your question)? In which case there is still a conflict with another directive or you are seeing a cached response.)
You will need to clear your browser cache after making this change as the 301 (permanent) redirect will be cached by the browser.
If you specifically want to redirect only /air-india-trial/ and not /air-india-trial/<something> then you need to use a RedirectMatch directive instead, which matches against a regex and is not prefix-matching. For example:
RedirectMatch 301 ^/air-india-trial/$ /trial/
Also, if you have any mod_rewrite (RewriteRule) redirects then these might conflict. Since different Apache modules runs independently and at different times throughout the request, you should avoid mixing redirects from both modules because of potential conflicts. (mod_rewrite will always run first on Apache 2.4, despite the apparent order of these directives in .htaccess.)

.htaccess redirect from subdirectory to another domains subdirectory accordingly

I am trying to make a redirect from my primary domain to an secondary domain, but only if the primary domain's request is to a sub directory.
The sub directory I want to redirect from is FTP, so if the user makes the following request:
http://www.site1.com/FTP/free/50b694124bd63/SaMple+PicTure.PnG
it would be transformed to
http://www.site2.com/FTP/free/50b694124bd63/SaMple+PicTure.PnG
but if the user makes a request that does not involve the FTP folder, the user will not be redirected. Like so:
http://www.site1.com or http://www.site1.com/somethingelse/
I am, however; a bit lost when it comes to making .htaccess files. What I have tried to do so far is:
# Redirect users
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^ftp(.*)$ http://site2.com/FTP/$1 [L,R=301]
</IfModule>
Any directions or samples would be great :)
No need to use the rewrite engine for simple redirects. I think you just want to use the Redirect directive:
Redirect /FTP http://www.site2.com/FTP
By default, this will result in a "temporary" redirect response (HTTP status 302). If you're sure the URL of the second site will never change, you can cause a "permanent" redirect response (HTTP status 301) by adding the permanent argument:
Redirect permanent /FTP http://www.site2.com/FTP
Also, note that the path of URLs is case-sensitive. If you want http://www.site1.com/ftp to also redirect, you will either need to add a rule with the lowercase path,
Redirect /ftp http://www.site2.com/FTP
or use mod_speling.

Resources