htaccess changing automaticly the RewriteRule - apache

I am trying to fix this error I had after adding these rules for HTTPS and non-www to .htaccess file on top
RewriteEngine on
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [L,NE,R=301,QSA]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule (.*) https://www.%{HTTP_HOST}%{REQUEST_URI} [L,NE,R=301,QSA]
it did fix the HTTPS and non-www but it messed this rule
RewriteRule ^product/([0-9]+)$ product.php?p=$1 [NC,L,QSA]
it's now changing this URL whenever there is no HTTPS or no www
from : www.example.com/product/2443
to : www.example.com/product.php/2443?p=2443
I want it to go back to this again:
www.example.com/product/2443

It sounds like you've put the directives in the wrong order. Your canonical redirects (HTTP to HTTPS and non-www to www) need to be at the top of the .htaccess file before your internal rewrites.
Also, since you are internally rewriting to a file with the same basename as the requested URL-path you will need to make sure that MultiViews is disabled to prevent mod_negotiation from internally issuing a subrequest for /product.php/2443 before mod_rewrite is able to process the request. For example, at the top of your .htaccess file add the following to ensure that MultiViews is disabled:
Options -MultiViews
You will need to clear your browser cache, since these erroneous (301 - permanent) redirects will have been cached by the browser.
Aside:
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [L,NE,R=301,QSA]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule (.*) https://www.%{HTTP_HOST}%{REQUEST_URI} [L,NE,R=301,QSA]
Reverse these two rules to avoid a double redirect when requesting HTTP + non-www. However, you should leave these as they are if you are planning to implement HSTS.
The QSA flag is not required on either of these rules.
And the capturing RewriteRule pattern (ie. (.*)) is superfluous. It would be more efficient to just use ^ (or $) - an assertion - since it only needs to be successful for everything, it doesn't need to actually match anything.

Related

htaccess http to https for domain and domain/paths

I need to convert all http access to a domain including its potential pages under the main page, to its https equivalent. What I have now just directs all Domain and www.Domain access to https://Domain but not pages that are off the main page. How can I modify the htaccess commands so in addition I can get http://Domain/other-web-pages to go to https://Domain/other-web-pages
RewriteEngine On
RewriteCond %{HTTPS} !on [OR]
RewriteCond %{HTTP_HOST} ^www\.example\.com$
RewriteRule (.*) https://example.com [L,R=301]
You are close. The redirection works, but you need to actually hand over the requested path too:
RewriteEngine On
RewriteCond %{HTTPS} !on [OR]
RewriteCond %{HTTP_HOST} ^www\.example\.com$
RewriteRule ^ https://example.com%{REQUEST_URI} [QSA,L,R=301]
Such general should get implemented in the actual http server's central host configuration. If you have no access to that you can use a distributed configuration file instaed. Such file has to be placed inside the http server's DOCUMENT_ROOT folder.
(An alternative to #arkascha's answer, that builds on your existing code.)
:
RewriteRule (.*) https://example.com [L,R=301]
You are already capturing the requested URL-path (ie. (.*)) but not passing this through to the substitution string. So, all you need is the corresponding backreference ($1) that contains the URL-path. For example:
:
RewriteRule (.*) https://example.com/$1 [L,R=301]
And, if you have other directives, this needs to go near the top of the .htaccess file before any existing rewrites.
You will need to clear your browser cache before testing, since any erroneous 301 (permanent) redirects to the homepage will have been cached by the browser. Test with 302 (temporary) redirects to avoid such caching issues.

Apache Redirect // to no //

An email went out with the wrong link (https://www.digitalmarketer.com/digital-marketing/content-marketing-strategy//) and we need to redirect the // to (https://www.digitalmarketer.com/digital-marketing/content-marketing-strategy/) but no matter what I try, the redirect isn't working.
They also want it to be redirected to always have https:///www at the beginning and to never have index.html at the end, so already in the .htaccess file I have:
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{HTTP_HOST} !^$
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTPS}s ^on(s)|
RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
RewriteRule ^content\-marketing\-strategy/index\.html$ /digital-marketing/content-marketing-strategy/? [L,R=301]
I've tried adding a new RewriteRule, but this won't work:
RewriteRule ^content\-marketing\-strategy//$ /digital-marketing/content-marketing-strategy/? [L,R=301]
I'm very new to Apache and redirects so any help is much appreciated! Thank you!
Edit: Of note, this is in an .htaccess file inside of the digital-marketing folder (https://www.digitalmarketer.com/digital-marketing/.htaccess) which was done so all the above rules would only apply to the digital-marketing folder.
You can use insert rule at the end of your other rules to strip multiple // into /:
RewriteCond %{THE_REQUEST} //
RewriteRule ^.*$ /digital-marketing/$0 [R=301,L,NE]
Apache automatically strips down multiple // into one inside the pattern for RewriteRule thus captured value $0 will have all // converted into /
You can write a wildcard expression to remove trailing slashes. The below will match any HTTP or HTTPS URL that trails in a forward slash, and remove all trailing forward slashes from that URL:
RewriteRule ^(.*)/+$ $1 [R=301,L]
And more using 301 redirects, see more here: Best Practice: 301 Redirect HTTP to HTTPS (Standard Domain)
Good luck!
I see nothing in the way that the rule is written that would make it not rewrite. However you have multiple rules with the L flag that might stop processing on the rewrite at an earlier point than you are looking for. From the documentation
The [L] flag causes mod_rewrite to stop processing the rule set. In most contexts, this means that if the rule matches, no further rules will be processed.
(https://httpd.apache.org/docs/current/rewrite/flags.html).
You can try this page out http://htaccess.mwl.be/ to test all your rules together. You might have to rewrite them a bit to work with that page, it's not aware of the level your .htaccess file is at so you will have to rewrite all your rules to trigger from the root for example: RewriteRule ^digital\-marketing/content\-marketing\-strategy//$ /digital-marketing/content-marketing-strategy/? [L,R=301]

.htaccess error - ERR_TOO_MANY_REDIRECTS

I have this .htaccess file to redirect http:// to https://
I also did www. to root domain redirection!
www. to root domain works! however https:// redirection doesn't!
If I set RewriteCond %{HTTPS} on to RewriteCond %{HTTPS} off or RewriteCond %{HTTPS} =!on I get a browser error:
The example.com page isn’t working
mysite.com redirected you too many times.
Try clearing your cookies.
ERR_TOO_MANY_REDIRECTS
One edit I did gave me a 500 error but I reverted that back to how it was before! all I did was change: RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} to RewriteRule(.*) https://%{HTTP_HOST}%{REQUEST_URI} or RewriteRule (.*)https://%{HTTP_HOST}%{REQUEST_URI}
Anyone have any Ideas on how to fix this issue?
This is my entire .htaccess file!
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
RewriteCond %{HTTPS} on [OR]
RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteRule ^ https://antimalwareprogram.co%{REQUEST_URI} [R=301,L,NE]
</IfModule>
RewriteCond %{HTTPS} on
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
Yes, this will create a redirect loop. The logic is wrong. What this says is... if HTTPS is "on" then redirect to HTTPS. You should be checking if HTTPS is "off" (or "not on", ie. !on).
(By removing the spaces between the arguments you likely created a rewrite loop, hence the 500 error. Spaces are delimiters in Apache config files.)
Try something like the following instead:
RewriteEngine On
RewriteCond %{HTTPS} !on [OR]
RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteRule ^ https://example.com%{REQUEST_URI} [R=302,L,NE]
This handles both the HTTPS and www canonical redirects. You don't need the first rule. You don't need the <IfModule> container either.
Change the 302 to 301 only when you are sure it's working OK.
Make sure you've cleared your browser cache before testing. 301s get cached hard by the browser.
UPDATE: If this still gives you the same error (a redirect loop) then it's possible that your SSL is managed by a front-end proxy, not your application server. If this is the case then you won't be able to use the HTTPS server variable. See these related questions:
http to https redirection through htaccess: incorrect redirection error
htaccess rewrite - too many redirects
It seems that in this case, ENV:HTTPS (an environment variable) needed to be used in place of HTTPS (Apache server variable). Note, however, that this is non-standard / server specific, as it implies a front-end proxy is being used to manage the SSL.

.htaccess www to non-www while keeping path, ssl, and query string

I'm trying to achieve the following in .htaccess:
redirect www to non-www
keep the path that has been requested (e.g. domain.com/path/to/file.php)
keep the query string, if any (e.g. domain.com/path/to/file.php?key=val&key2=val2)
keep the protocol that has been requested (either http or https)
I tried this code:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^domain\.com$ [NC]
RewriteRule ^(.*)$ http://domain.com/$1 [R=301,L]
but it doesn't respect http/https, nor does it keep the path that has been requested (e.g. it redirects https://www.domain.com/path/file.php to http://domain.com/file.php, when it should redirect to https://domain.com/path/file.php instead.)
Any ideas?
You need to check if a request is http or https, and redirect to the correct protocol. Right now, you're redirecting everything to http://. You can use this condition and grouping:
RewriteCond %{HTTPS}:s (on:(s)|off:s)
This uses the %{HTTPS} variable, which is either "on" or "off", and pairs it with an s. Then we match either on:(s) or off:s. Therefore, if HTTPS is "on", the (s) gets grouped and we can backrefernce it using a %2, otherwise, if HTTPS is "off", nothing gets grouped and %2 is blank. We can then use it in your redirect:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^domain\.com$ [NC]
RewriteCond %{HTTPS}:s (on:(s)|off:s)
RewriteRule ^(.*)$ http%2://domain.com/$1 [R=301,L]
As for the second thing about the paths not being preserved, it sounds like your rules are in the directory /path, which means it gets stripped off when it gets sent through these rules. You need to move them to your document root. Alternatively, if you must have your rules in the /path directory, you can use the %{REQUEST_URI} variable instead:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^domain\.com$ [NC]
RewriteCond %{HTTPS}:s (on:(s)|off:s)
RewriteRule ^ http%2://domain.com%{REQUEST_URI} [R=301,L]

Apache Rewrite: *FULL* non-www to www permanent redirect

I'm having difficulty constructing a non-www to www permanent redirect. Here are the conditions I need to meet...
1.) Multi-domain support (multiple website domains pointed at the same directory on a server with their own dedicated copy of a database). This means using %{HTTP_HOST} and NOT a static domain name.
2.) ALL possible pathes (or at least standards compliant ones) need to redirect; this means directories, HTTP queries, etc.
3.) I'm using shared hosting, so I have access to the .htaccess file only.
Here is what I'm working with right now:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^%{HTTP_HOST}
RewriteRule (.*) http://www\.%{HTTP_HOST}/$1 [R=301,L]
Details:
I'm testing with the browser cache disabled just in case.
I've removed other syntax during testing to see what replies work in their own right.
Here you go:
RewriteCond %{HTTP_HOST} !^www\.
RewriteCond %{HTTPS}s on(s)|
RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
It will also keep the URI scheme.
This here works for me (with my domainname)
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^%{HTTP_HOST}
RewriteRule ^(.*)$ http://www\.%{HTTP_HOST}/$1 [R=301,L]
The only difference I see is the matching pattern at the RewriteRule.