Getting HTTPS to HTTP redirection for URLs without trailing slash - apache

https://<domain_name>/faq for this url i am getting 301 http redirection resulted :
Result
CONGRATULATION. Everything seems to be fine.
https://<domain_name>/faq
301 Moved Permanently
http://<domain_name>/faq/
301 Moved Permanently
https://<domain_name>:443/faq/
200 OK
but if use https://<domain_name>/faq/ for this url i am getting 200 Ok without any redirection (working fine) Resulted:
Result
CONGRATULATION. Everything seems to be fine.
https://<domain_name>/faq/
200 OK
Please suggest some solution to it , it should not redirect in case of without trailing slash
Tried using DirectorySlash Directive in apache conf but it resulted in 403 error

Related

Prevent wildcard in htaccess 301 redirect

I have setup a htaccess 301 redirect to redirect an indexed page to a new page, however this is also acting as a wildcard redirect for child pages which I do no what to happen.
Old structure
example.com/faq
example.com/faq/question-1
example.com/faq/question-2
etc etc
New structure
example.com/faqs
example.com/faq/question-1
example.com/faq/question-2
etc etc
htaccess redirect in place :
Redirect 301 /faq/ https://example.com/faqs/
This is working with no issues to send /faq to /faqs however it is also sending /faq/* to /faqs/* which I do not want to happen.
For example going to example.com/faq/question-1 causes a to many redirects error and finally lands on example.com/faqs/question-1
Is there anything i can add to the single redirect line to prevent this happening, or is their a more complex use of RewriteRule I could use instead. Research into the matter initially seem to confirm that this should/would happen, and if it does what can be added to prevent it. After a prompt to the apache docs I can see why they would redirect as a wildcard.
After suggests from CBroe an implementation of using RedirectMatch worked.
RedirectMatch 301 ^/faq/$ https://example.com/faqs
This now redirects /faq to /faqs, however doesn't redirect /faq/question-1 to /faqs/question-1 etc

How to 404 urls in .htaccess?

I'm trying to make all urls for https://www.example.com/page/ redirect to 404.
The problem I'm having is every number after /page/ is redirecting to the homepage, and is getting indexed - causing a duplicate content penalty.
So far I have around 30 pages indexed, which all redirect to the homepage.
I'd like to have any URL which has a number after /page/ to 404 (with the 404 header), so I can deindex all of these pages.
So far I've tried:
Redirect 404 ^page/(.*)$
or
Redirect 404 /page/*
Unsurprisingly these haven't worked - where am I going wrong?
Redirect 404 ^page/(.*)$ or Redirect 404 /page/*
You are close, except that the Redirect directive uses simple prefix-matching, it does not use a regular expression (regex) or wildcard match.
to make all urls for https://www.example.com/page/ redirect to 404.
To make all requests for /page/ result in a 404, regardless of whether a number follows it or not, you would use the following:
Redirect 404 /page/
As noted above, the Redirect directive uses simple prefix-matching, so the above serves a 404 for any URL that starts with /page/.
However ....
any URL which has a number after /page/ to 404
To specifically serve a 404 for URLs of the form /page/<number> only and not /page/ then you would need to use a RedirectMatch directive instead that uses regex to match and not prefix-matching.
For example:
RedirectMatch 404 ^/page/\d+$
\d+$ matches 1 or more digits to the end of the URL-path. So the above will match /page/1 and /page/123456 but not /page/ or /page/abc or /page/123z etc.
If instead you wanted to match /page/<something> where <something> is literally anything, but not match /page/ only. Then you could instead use the following:
RedirectMatch 404 ^/page/.
The above matches /page/ at the start of the URL-path followed by at least 1 other character.
Note that whilst we are using the Redirect and RedirectMatch directives here, there is no external redirect (3xx response). The 404 is served by Apache as an internal subrequest. Apache sends the 404 Not Found header.
However, if you specifically want to "deindex" these pages quicker then consider sending a "410 Gone" instead. This is a stronger signal for search engines that the page is not coming back. In this case you can also use the gone keyword in place of the 410 status code.

Redirect and append GET-Parameter

I'm having trouble with redirecting from one URL to another, while appending one GET-parameter to the NEW URL.
The first time (from root of old domain to root of new domain) works perfectly fine. After (further down in the .htaccess this one case) the GET-parameter appears randomly in the middle of the new url.
Obviously that leads to some nasty 404 situations...
RewiriteEngine on
Redirect 301 / https://foo.bar?redirect=1
Redirect 301 /foo/bar/ https://foo.bar/foo/bar?redirect=1
To give further information:
The idea is to redirect inklusive this parameter, to trigger a popup, giving information about the recent redirect so the user doesn't lose his confidence about the visit of foo.bar.
The first redirect will result in just the right way, it works perfectly.
The second thou, turns out like:
https://foo.bar/foo/?redirect=1bar/
Please and Thank you :)
Your rules work fine as you configure them , why ?
With mod_alias redirect and this part
Redirect 301 / https://foo.bar?redirect=1
Will match / & /foo/ & /foo/bar/ and so on , so the second rule will not work at all because of every request being captured by firrt rule ,and the result of redirecting /foo/bar/ with first rule is foo/bar?redirect=1foo/bar/ because with redirect every things after match will be appending to new target.
To avoid that use RedirectMatch
RewriteEngine On
RedirectMatch 301 /?$ https://foo.bar/foo/bar?redirect=1
RedirectMatch 301 /foo/bar/ https://foo.bar/foo/bar?redirect=1
By this way with regex , you will be able to match against root only by first rule then against /foo/bar/ by second rule.
Note: clear browser cache then test

Rewrite language code into url if missing

I want to change an existing Magento store to add store/lang codes to the url i.e.
http://mystore/en/PRODUCTXYZ.html
http://mystore/de/PRODUCTXYZ.html
Old links to http://mystore/PRODUCTXYZ.html will now throw a 404 error.
How can I create an Apache url rewrite rule to add a language code if it is missing i.e. rewrite
http://mystore/PRODUCTXYZ.html
to
http://mystore/de/PRODUCTXYZ.html
So that old links 301 redirect to the correct product.
I have worked around this with
Redirect 301 /PRODUCTXYZ http://mystore/de/PRODUCTXYZ.html
But obviously for thousands of products this might not be practical.
You can redirect multiple Product.html urls with just 1 line of code using RedirectMatch :
RedirectMatch 302 ^/([^/.]+)\.html$ http://example.com/de/$1.html
I used 302 for testing purposes and to avoid browser's cache,
change 302 to 301 (permanent redirect) when you are sure the redirect is working.

301 redirect with {~`% characters in url

Trying to apply a 301 redirect to this url "/Qx%40w%2C6M42VAwp3%40Rb%7B~cC4ure%60QWI9" but it's not liking it, i'm guessing because of the horrible url and all the strange characters in it, any ideas?
This is the code I currently have:
redirect 302 /Qx%40w%2C6M42VAwp3%40Rb%7B~cC4ure%60QWI9 /product-tool-case.php
You can do this with mod_rewrite as follows:
RewriteRule ^/Qx%40w%2C6M42VAwp3%40Rb%7B~cC4ure%60QWI9 /product-tool-case.php
I?f you want to continue using mod_alias as above, then you should ensure that the old url is url-decoded. So in the above example try:
redirect 302 /Qx#w,6M42VAwp3#Rb{~cC4ure`QWI9 /product-tool-case.php