Earlier the admin RewriteRule was effecting scripts/admin.js until I added the RewriteCond.
Then I noticed that scripts/members.js was being effected by the same rule with the member context.
I added the exact same RewriteCond after as is below...
RewriteEngine on
RewriteCond %{REQUEST_URI} !\.(css|js|zip)$
RewriteRule .*/admin(.+) admin$1 [QSA]
RewriteCond %{REQUEST_URI} !\.(css|js|zip)$
RewriteRule .*/members(.+) members$1 [QSA]
Instead of having to manually add a RewriteCond every time to deal with issues how can I merge these rules please?
RewriteRule .*/admin(.+) admin$1 [QSA]
RewriteRule .*/blog(.+) blog$1 [QSA]
RewriteRule .*/forums(.+) forums$1 [QSA]
You can merge your three checks into one like this:
RewriteCond %{REQUEST_URI} !\.(css|js|zip)$
RewriteRule .*/(admin|blog|forms)(.+) $1$2 [QSA]
But it does seem like a rule to simply stop processing for js/css files will be a better option for you, unless you actually need rules applied to them for some reason.
You can invert the condition and make a pass-through rule that essentially tells mod_rewrite to stop. This way, you have 1 RewriteCond, 1 extra RewriteRule that is applied via the condition, and then everything else:
# Just one of these, note that there is no !
RewriteCond %{REQUEST_URI} \.(css|js|zip)$
# pass through, if the request is for a css/js/zip, stop rewriting
RewriteRule ^ - [L]
# otherwise, do some rewriting
RewriteRule .*/admin(.+) admin$1 [QSA]
RewriteRule .*/blog(.+) blog$1 [QSA]
RewriteRule .*/forums(.+) forums$1 [QSA]
This will, however, affect any other rules you have that could apply to requests to css/js/zip, because this will cause all requests for those extensions to bypass all rewriting.
Related
I want to combine these two rules, but not sure how
RewriteRule ^([^\.]+)$ $1.html [L]
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]
when I put both, I get the error "too many redirects"
My goal here is to combine them both,
the first rule is to remove file extensions (ex. html)
the second rule is: make every URL go to https://www.example.com, rather than https://example.com
With the additional information you now gave I would suggest this approach:
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\.example\.com$
RewriteRule ^ https://www.example.com%{REQUEST_URI} [R=301,END]
RewriteRule ^/?(.+)\.html$ /$1 [R=301,END]
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^ %{REQUEST_URI}.html [END]
In general it is a good diea to start using a R=302 temporary redirection and only change that to a R=301 permanent redirection once everything works as expected. That prevents nasty caching issues ...
On the side I have about 400,000 subdomains. in view of the above, some calls also operate subdomain level, e.g..
subdomain1.example.com/some_action/
Such actions that should be performed only from the domain have 27.
Htaccess file I created a rule:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www.)?example.com
RewriteRule ^some_action1/?$ index.php?some_action1 [L]
If i add this line
RewriteRule ^some_action2/?$ index.php?some_action2 [L]
not work in the same way as for some_action3, etc.
I have added for each action separately
RewriteCond %{HTTP_HOST} ^(www.)?example.com
Can you somehow skip to harmonize?
Each RewriteCond condition only applies to the immediately following RewriteRule. That means if you have a bunch of rules, you have to duplicate the conditions. If you really don't want to do that for some reason, you could use a negate at the very beginning of all your rules. This may or may not be a good solution since it could affect how you make future changes to the rules:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^(www.)?example.com
RewriteRule ^ - [L]
RewriteRule ^some_action1/?$ index.php?some_action1 [L]
RewriteRule ^some_action2/?$ index.php?some_action2 [L]
RewriteRule ^some_action3/?$ index.php?some_action3 [L]
etc...
So the first rule checks for the negative of the host being example.com, and skips everything. Then you can add all your rules without having to worry about that condition.
However, if your "some_action" is always going to be part of the GET parameters, you can maybe just use a single rule:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www.)?example.com
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/?$ index.php?$1 [L]
I have written a simple mod rewrite script to turn website.com/index.php?var1=1&var2=2&var3=3 into website.com/index/1/2/3. For some reason it's not working, any ideas why?
#RewriteRule ^$ index.php [QSA]
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.+)$ $1\.php [QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ index.php/$1 [QSA]
RewriteRule ^([a-zA-Z0-9]+)/([a-zA-Z0-9]+)/([a-zA-Z0-9]+)$ comments.php?var1=$1&var2=$2&var3=$3
The first rule rewrites index/1/2/3 to index/1/2/3.php.
then the second rule rewrites this to index.php/index/1/2/3.php
and finally the last rule doesn't match.
If you want to rewrite website.com/index.php?var1=1&var2=2&var3=3 into website.com/index/1/2/3, you must capture the query string arguments with RewriteCond and insert them into a RewriteRule
RewriteEngine on
RewriteCond %{QUERY_STRING} var1=(.*?)&var2=(.*?)&var3=(.*?)
RewriteRule ^index.php$ /index/%1/%2/%3 [L]
If you want to redirect instead of internally rewrite, i.e. show the new URL in the browser, use [R,L] as RewriteRule flags
RewriteRule ^index.php$ /index/%1/%2/%3 [R,L]
It looks like the rewrite on line 6 will catch that and send it to index.php. It's hard to tell not knowing what things are files and directories, but I would guess that it isn't. If you have access to the httpd.conf you can turn up the rewrite logging, set
RewriteLogLevel 8
And it will log far more information than you need to debug this. On newer versions of apache the setting is set with LogLevel e.g..
LogLevel alert rewrite:trace8
which may be allowed in vhosts or htaccess so look it up in your version's docs.
Don't leave this on as it will seriously impact performance.
For the past few hours (days) I have been having some trouble redirecting a
page to SSL.
My setup is as follows: I have the following .htaccess for an e-commerce site
on Apache 2.2.16 on Debian (all required mods enabled)
RewriteEngine On
RewriteBase /shop
RewriteCond $1 !^(index\.php|products|img|theme\.php|checkout\.php)
RewriteRule ^(.*)$ index.php?/$1 [L]
all requests are passed to index.php which acts as my controller and includes
other .php files as necessary.
I now want to use HTTPS for the checkout process which is a php script
cleverly called checkout.php
I thought it would be as easy as changing my .htaccess to:
RewriteEngine On
RewriteBase /shop
RewriteCond %{SERVER_PORT} !^443$
RewriteCond %{SERVER_URI} checkout\.php
RewriteRule ^checkout.php?/$1 https://localhost/shop/checkout.php?/$1 [L,R]
RewriteCond $1 !^(index\.php|products|img|theme\.php|checkout\.php)
RewriteRule ^(.*)$ index.php?/$1 [L]
so that checkout.php is not processed by index.php.
Apparently it is not that simple. I could probably do it by using a hardcoded
https link to checkout but I would prefer to do it with mod_rewrite.
If anyone can share some insight into
this it would be really appreciated.
Thanks in advance
There are a few problems. First, the pattern in your first RewriteRule
RewriteRule ^checkout.php?/$1 https://localhost/shop/checkout.php?/$1 [L,R]
is written incorrectly. $1 isn't meaningful there (it's a capture result, but no capture has happened yet), and also the query string (part of the request after the ?) isn't part of what's matched, as the RewriteRule documentation says.
Second, I think you meant to use REQUEST_URI instead of SERVER_URI.
So I think you want something like this:
RewriteCond %{HTTPS} off
RewriteCond %{REQUEST_URI} ^/checkout\.php
RewriteRule .* https://localhost/shop/checkout.php [L,R]
RewriteCond %{REQUEST_URI} !^/(index\.php|products|img|theme\.php|checkout\.php)
RewriteRule ^(.*)$ /index.php?/$1 [L]
A few notes:
You don't need to match or add back in the query string in the first RewriteRule; mod_rewrite will automatically add it back in.
It's conventional to test RewriteCond %{HTTPS} off instead of
RewriteCond %{SERVER_PORT} !443, as #Jon Lin suggests.
You may want to add the QSA flag in your second RewriteRule.
I have following command in my .htaccess
RewriteCond %{HTTP_HOST} ^(www\.)?([a-z0-9-]+)\.example\.com [NC]
RewriteRule ^(.*?)-([a-z]+) %2/$1.$2 [L]
RewriteRule ^(.*?)-([0-9]+)([a-z]) %2/$1$3.$2 [L]
%2 is not working in second and later lines.
Can I define any variable for %2 and use it in all RewriteRule commands?
Following command works
RewriteCond %{HTTP_HOST} ^(www\.)?([a-z0-9-]+)\.example\.com [NC]
RewriteRule ^(.*?)-([a-z]+) %2/$1.$2 [L]
RewriteCond %{HTTP_HOST} ^(www\.)?([a-z0-9-]+)\.example\.com [NC]
RewriteRule ^(.*?)-([0-9]+)([a-z]) %2/$1$3.$2 [L]
But I want use %2 for multiple rule line without duplicating condition.
You can use the RewriteRule flag S|skip to tie multiples RewriteRules to a single RewriteCond (or to a set of RewriteConds). Here is an example that applies one Cond to three Rules:
RewriteCond %{HTTP_HOST} !^www.mydomain.com$
# skip rules if NOT within domain - only way to tie multiple rules to one cond
RewriteRule .? - [S=3]
RewriteRule ^path1(/.*)$ /otherpath1$1
RewriteRule ^path2(/.*)$ /otherpath2$1
RewriteRule ^path3(/.*)$ /otherpath3$1
To change an existing Cond to work for multiple Rules you have to:
Negate the condition (prepend it with !)
If you have multiple RewriteConds: Change logical ANDs in the Conds to ORs and vice versa.
Add a skipping rewrite rule in front of all rules that you want to tie to the condition(s). Set the S parameter to the number of Rule lines to be skipped.
Please be aware that it is not possible to use any backreferences that point back to the RewriteCond (like %1) in the skipped Rules. These are only accessible in the skipping RewriteRule.
The variable must be saved as an Apache var, then that can be used without repeated conditions.
Saving in Apache variables are shown in second line. Usage of saved vars in 3th and 4th lines.
RewriteCond %{HTTP_HOST} ^(www\.)?([a-z0-9-]+)\.example\.com [NC]
RewriteRule .? - [E=Wa:%1,E=Wb:%2]
RewriteRule ^(.*?)-([a-z]+) %{ENV:Wb}/$1.%{ENV:Wb} [L]
RewriteRule ^(.*?)-([0-9]+)([a-z]) %{ENV:Wb}/$1$3.$2 [L]
Clearly, this isn’t much fun, especially as things grow and become more complex. However, there is a less well known option of the RewriteRule statement, that tells apache to skip the next N number of RewriteRule statement. [S=N]. So instead of checking each time if the request is NOT a file AND is NOT a directory, we could do this:
Code block
#
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule .* - [S=3]
RewriteRule ^([^./]+)/$ http://www.santweb.co.uk/$1 [L]
RewriteRule ^([^./]+)/([^./]+)/$ http://www.santweb.co.uk/$1/$2 [L]
RewriteRule ^([^./]+)/([^./]+)/([^./]+)/$ http://www.santweb.co.uk/$1/$2/$3 [L]
#
I found this, from: http://www.sant-media.co.uk/2010/03/applying-rewritecond-to-multiple-rewriterule-in-htaccess/
I think it helpfull
From Apache 2.4 you can use the <If> directive:
RewriteEngine On
<If "%{HTTP:Upgrade} == 'websocket'">
RewriteRule /nidoran/(.*) ws://localhost:8080/nidoran/$1 [P,L]
RewriteRule /kakuna/(.*) ws://localhost:8081/kakuna/$1 [P,L]
RewriteRule /porygon/(.*) ws://localhost:8082/porygon/$1 [P,L]
</If>
http://httpd.apache.org/docs/2.4/expr.html#examples