.htaccess RewriteRule - Difference between [L,R] and [R,L]? - apache

Is there any difference between flag [L,R] and [R,L] e.g:
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !newdomain.com$ [NC]
RewriteRule ^(.*)$ http://newdomain.com/$1 [L,R=301]
vs
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !newdomain.com$ [NC]
RewriteRule ^(.*)$ http://newdomain.com/$1 [R=301,L]
?

No there is no difference in 2 rules.
Order of various flags in RewriteRule doesn't matter.
Reference: httpd.apache.org/docs/current/rewrite/flags.html

Related

htaccess one RewriteCond for multiple RewriteRules?

I have trouble with redirects in .htaccess.
I have several domains (example.com, example.net, example.uk). I have set of RewriteRules for each domain. But for some reason, the redirects works for all domains. Following is my tried htaccess rules file.
RewriteCond %{HTTP_HOST} ^(www\.example\.com|example\.com) [NC]
RewriteRule ^1example$ / [R=302,L]
RewriteRule ^2example$ /example2 [R=302,L]
RewriteRule ^3example$ / [R=302,L]
RewriteRule ^4example$ /example4 [R=302,L]
RewriteCond %{HTTP_HOST} ^(www\.example\.net|example\.net) [NC]
RewriteRule ^5example$ /sgsg [R=302,L]
RewriteRule ^6example$ /example2 [R=302,L]
RewriteRule ^7example$ / [R=302,L]
RewriteRule ^8example$ /example44 [R=302,L]
RewriteCond %{HTTP_HOST} ^(www\.example\.uk|example\.uk) [NC]
RewriteRule ^9example$ / [R=302,L]
RewriteRule ^10example$ /example12 [R=302,L]
RewriteRule ^11example$ / [R=302,L]
RewriteRule ^12example$ /example41 [R=302,L]
Do you have any idea how to separate RewriteRules only for specific domain?
With your shown samples and attempts; please try following .htaccess rules file. As per anubhava's comments have added RewriteCond conditions before each RewriteRule.
Please make sure to clear your browser cache before testing your URLs.
RewriteEngine ON
RewriteCond %{HTTP_HOST} ^(?:www\.)?example\.com$ [NC]
RewriteRule ^(?:1|3)example$ / [R=302,L,NC]
RewriteCond %{HTTP_HOST} ^(?:www\.)?example\.com$ [NC]
RewriteRule ^(2|4)example$ /example$1 [R=302,L,NC]
RewriteCond %{HTTP_HOST} ^(?:www\.)?example\.net$ [NC]
RewriteRule ^5example$ /sgsg [R=302,L,NC]
RewriteCond %{HTTP_HOST} ^(?:www\.)?example\.net$ [NC]
RewriteRule ^6example$ /example2 [R=302,L,NC]
RewriteCond %{HTTP_HOST} ^(?:www\.)?example\.net$ [NC]
RewriteRule ^7example$ / [R=302,L,NC]
RewriteCond %{HTTP_HOST} ^(?:www\.)?example\.net$ [NC]
RewriteRule ^8example$ /example44 [R=302,L,NC]
RewriteCond %{HTTP_HOST} ^(?:www\.)?example\.uk$ [NC]
RewriteRule ^9example$ / [R=302,L,NC]
RewriteCond %{HTTP_HOST} ^(?:www\.)?example\.uk$ [NC]
RewriteRule ^10example$ /example12 [R=302,L,NC]
RewriteCond %{HTTP_HOST} ^(?:www\.)?example\.uk$ [NC]
RewriteRule ^11example$ / [R=302,L,NC]
RewriteCond %{HTTP_HOST} ^(?:www\.)?example\.uk$ [NC]
RewriteRule ^12example$ /example41 [R=302,L,NC]
Fixes applied to OP's attempts:
Attached 2 rules into 1 eg: RewriteRule ^(?:1|3)example$ / [R=302,L,NC] very first rule in above file.
Added NC flag in both RewriteCond and RewriteRule in all of the rules.
Made use of non-capturing group in RewriteCond to make www. optional eg: FROM ^(www\.example\.com|example\.com) TO ^(?:www\.)?example\.com$
As already mentioned, the RewriteCond (condition) directive itself applies only to the first RewriteRule that follows. The RewriteCond directive forms part of a single rule.
However, you can "workaround" this in various ways, without having to apply the same condition to multiple rules.
For example:
# Skip the following 4 rules when host is NOT "example.com"
RewriteCond %{HTTP_HOST} !^(www\.)?example\.com [NC]
RewriteRule ^ - [S=4]
# These rules only apply to "example.com"
RewriteRule ^1example$ / [R=302,L]
RewriteRule ^2example$ /example2 [R=302,L]
RewriteRule ^3example$ / [R=302,L]
RewriteRule ^4example$ /example4 [R=302,L]
# Skip the following 4 rules when host is NOT "example.net"
RewriteCond %{HTTP_HOST} !^(www\.)?example\.net [NC]
RewriteRule ^ - [S=4]
# These rules only apply to "example.net"
RewriteRule ^5example$ /sgsg [R=302,L]
RewriteRule ^6example$ /example2 [R=302,L]
RewriteRule ^7example$ / [R=302,L]
RewriteRule ^8example$ /example44 [R=302,L]
# Skip the following 4 rules when host is NOT "example.uk"
RewriteCond %{HTTP_HOST} !^(www\.)?example\.uk [NC]
RewriteRule ^ - [S=4]
# These rules only apply to "example.uk"
RewriteRule ^9example$ / [R=302,L]
RewriteRule ^10example$ /example12 [R=302,L]
RewriteRule ^11example$ / [R=302,L]
RewriteRule ^12example$ /example41 [R=302,L]
Alternatively, you could temporarily prefix the hostname to the URL-path and test for this directly in the RewriteRule directive.
For example:
# Prefix the hostname to the URL-path
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{HTTP_HOST} ^(?:www\.)?(example\.com|example\.net|example\.uk)
RewriteRule ^ %1%{REQUEST_URI}
RewriteRule ^example\.com/1example$ / [R=302,L]
RewriteRule ^example\.com/2example$ /example2 [R=302,L]
RewriteRule ^example\.com/3example$ / [R=302,L]
RewriteRule ^example\.com/4example$ /example4 [R=302,L]
RewriteRule ^example\.net/5example$ /sgsg [R=302,L]
RewriteRule ^example\.net/6example$ /example2 [R=302,L]
RewriteRule ^example\.net/7example$ / [R=302,L]
RewriteRule ^example\.net/8example$ /example44 [R=302,L]
RewriteRule ^example\.uk/9example$ / [R=302,L]
RewriteRule ^example\.uk/10example$ /example12 [R=302,L]
RewriteRule ^example\.uk/11example$ / [R=302,L]
RewriteRule ^example\.uk/12example$ /example41 [R=302,L]
# (OPTIONAL) Remove hostname prefix from URL-path
RewriteRule ^[^/]+/(.*) $1

Apache rewrite rule with empty get args

I need to redirect all urls with ? at the end. For example: http://example.com/abc?, but no http://example.com/abc?a=5 or http://example.com/abc.
This doesn`t work:
RewriteEngine on
RewriteBase /
RewriteRule ^(.*)\?$ http://newdomain.com/$1 [R=301,L]
or
RewriteEngine on
RewriteBase /
RewriteCond %{THE_REQUEST} \?
RewriteCond %{QUERY_STRING} ^$
RewriteRule ^(.*)$ http://newdomain.com/$1 [R=301,L]
Try :
RewriteEngine on
RewriteBase /
RewriteCond %{THE_REQUEST} \?\sH
RewriteRule ^(.*)$ http://domain.com/$1? [R=301,L]
You can use this rule:
RewriteCond %{THE_REQUEST} \?\sHTTP
RewriteRule ^ %{REQUEST_URI}? [NE,R=301,L]

htaccess multiple rules and http to https

I'm trying figure out the rewrite rules. We currently have the following rule:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(blog\.)?example\.com$ [NC]
RewriteCond %{REQUEST_URI} !^/(public|scripts)/ [NC]
RewriteRule ^(.*)$ /public/$1 [L]
</IfModule>
And we need an additional rule that redirects all HTTP traffic to HTTPS.
I've tried the following, but won't work.
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{HTTPS} !on
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
RewriteCond %{HTTP_HOST} ^(blog\.)?example\.com$ [NC]
RewriteCond %{REQUEST_URI} !^/(public|scripts)/ [NC]
RewriteRule ^(.*)$ /public/$1 [L]
</IfModule>
Try adding a L flag to your redirect rule:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{HTTPS} !on
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [L,R]
RewriteCond %{HTTP_HOST} ^(blog\.)?example\.com$ [NC]
RewriteCond %{REQUEST_URI} !^/(public|scripts)/ [NC]
RewriteRule ^(.*)$ /public/$1 [L]
</IfModule>

Bad Flag Delimiters on .htacces

I've entered the following into my .htaccess and I'm getting the dreaded bad flag delimiters error. I've tried validators, and one says it's lines 5 and 7, another says all the other lines.
Any help would be appreciated.
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^example.com
RewriteRule (.*) http://www.example.com/$1 [R=301,L]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9} /index.php HTTP/
RewriteRule ^index.php$ http://www.example.com/ [R=301,L]
RewriteCond %{QUERY_STRING} ^source= RewriteRule (.*) /$1? [R=301,L]
RewriteRule ^category/([^/.]+)/?$ category.php?id=$1 [L]
RewriteRule ^business/([^/.]+)/?$ business.php?id=$1 [L]
RewriteRule ^event/([^/.]+)/?$ event.php?id=$1 [L]
Space is not allowed unescaped. Try this code:
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^example\.com$
RewriteRule (.*) http://www.example.com/$1 [R=301,L]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index.php\ HTTP/
RewriteRule ^index.php$ / [R=301,L]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /category.php\?id=([^&\ ]+) HTTP/
RewriteRule ^ /category/$1? [R=301,L]
RewriteCond %{QUERY_STRING} ^source=
RewriteRule (.*) /$1? [R=301,L]
RewriteRule ^category/([^/.]+)/?$ category.php?id=$1 [L]
RewriteRule ^business/([^/.]+)/?$ business.php?id=$1 [L]
RewriteRule ^event/([^/.]+)/?$ event.php?id=$1 [L]

Simple .htaccess Rewrite of a Param

I'm trying to get a param to rewrite to a directory, nothing is happening.
Example of what I want:
Rewrite From: http://domain.com/results.php?s=abc
Rewrite To: http://domain.com/results/abc
This is what I have, there is a www redirect to non-www, and that part is working fine, but my param rewrite is doing nothing.
I'm on Apache 2.4.
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
RewriteRule ^results/([^/]*)$ /results.php?c=$1 [L]
Ideas? Suggestions?
You can try:
Options -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^ http://%1%{REQUEST_URI} [R=301,L,NE]
RewriteCond %{THE_REQUEST} \s/+results\.php\?c=([^\s&]+) [NC]
RewriteRule ^ /results/%1? [R=301,NE,L]
RewriteRule ^results/([^/]+)/?$ results.php?c=$1 [L,QSA]