Multiple RewriteRules for a single set of RewriteConds - apache

We've tried all the suggestions on stackoverflow we could find for adding multiple RewriteRules for the same set of RewriteConds, but in all cases, we're only able to get the first RewriteRule to work.
This is the working code for a single rewrite that we'd like to add more categories to:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /designers/$1 [NC,L]
</IfModule>
None of the below work:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /designers/$1 [NC,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /labels/$1 [NC,L]
</IfModule>
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f [OR]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .? - [S=2]
RewriteRule ^(.*)$ /designers/$1 [NC,L]
RewriteRule ^(.*)$ /labels/$1 [NC,L]
</IfModule>

Your [OR] cond is almost correct. When you do a logical invert, you must also negate.
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule .? - [S=2]
RewriteRule ^(.*)$ /designers/$1 [NC,L]
RewriteRule ^(.*)$ /labels/$1 [NC,L]
So you don't want !-f, you want -f. Keep in mind that the S=2 skips 2 rules. If you have more categories you'll need to change that, or just use the L flag instead.

Related

.htaccess file not working properly in ionos. My website- https://rentocure.com

I want to have clean link like- https://rentocure.com/doctors/rc635502e5e9376 instead of https://rentocure.com/doctors.php?uid=rc635502e5e9376 (U can go through my website). I have the following code in localhost
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^([^/]+)/$ $1.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/)$
RewriteRule ^(.*)/$ $1 [L,R=301]
RewriteRule ^/?home/([^/]+)?$ "index.php" [L,QSA]
ErrorDocument 404 https://rentocure.com/404/
RewriteRule ^doctors/([^/]*)$ doctors.php?uid=$1 [L]
RewriteRule ^hospitals/([^/]*)$ hospitals.php?uid=$1 [L]
which is working fine in localhost but not in ionos apache server so I changed it to the following:
Options +MultiViews
Options +FollowSymlinks
RewriteEngine On
RewriteBase /
#Redirect to SSL Secured Page
RewriteCond %{SERVER_PORT} !=443
RewriteRule ^(.*)$ https://rentocure.com/$1 [R=301,L]
#Removing File Extension .php and adding trailing slash
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/$ $1.php
RewriteRule ^([^/]+)/([^/]+)/$ /$1/$2.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/)$
RewriteRule (.*)$ /$1/ [R=301,L]
#Custom 404 Page
ErrorDocument 404 https://rentocure.com/404/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^doctors/([^/]*)$ doctors/?uid=$1 [QSA,L]
But still it is not working kindly check my website and give me the solution. For the one whose solution will work for me will get assured cash reward from me.
Expecting solution through .htaccess code.

Different .htaccess rewrite rules depending on a $_GET param

Is there a way to make different rewrite rules in .htacess if a certain GET parameter was passed?
For example, if query string is:
htttp://domain.com/?debug=1, than .htaccess should look like:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^$ app/webroot/ [L]
RewriteRule (.*) app/webroot/$1 [L]
</IfModule>
If there is no debug=1 in query string, than:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule !\.(js|ico|gif|jpg|png|css|html|swf|flv|xml)$ index.php?$1 [QSA,L]
You can use RewriteCond like this:
RewriteEngine on
# ?debug=1 is present
RewriteCond %{QUERY_STRING} (^|&)debug=1\b [NC]
RewriteRule (.*) app/webroot/$1 [L]
# ?debug=1 is not present
RewriteCond %{QUERY_STRING} !(^|&)debug=1\b [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule !\.(js|ico|gif|jpg|png|css|html|swf|flv|xml)$ index.php?$1 [QSA,L]
You want to match against the query string in a RewriteCond:
RewriteCond %{QUERY_STRING} ^debug=1$ [NC]
RewriteRule (.*) app/webroot/$1 [L]
For more information, see the docs or this example.

htaccess trailing slash not working

I have the below code in my htaccess. The site works fine (ie: no 500 errors) but it is not adding the trailing slash to my URLs:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond $1 !\.(gif|jpe?g|png)$ [NC]
RewriteRule ^(.*)$ /index.php?$1 [NC,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ http://staging.foo.com/$1/ [L,R=301]
</IfModule>
What do I have wrong here?
You need to swap the order of your rules
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ http://staging.foo.com/$1/ [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond $1 !\.(gif|jpe?g|png)$ [NC]
RewriteRule ^(.*)$ /index.php?$1 [NC,L]
</IfModule>
The reason why it wasn't working was because the routing to /index.php rule was being applied first, then the rewrite engine loops, and then the 2nd rule (the redirect rule) won't get applied because the URI has been rewritten to /index.php and that causes the %{REQUEST_FILENAME} !-f to fail, since index.php exists, thus, the redirect never happens.

Excluding filename using RewriteRule

I want to pipe all URIs into index.php UNLESS the specific file exists in the root. I'm a little stumped when it comes to excluding index.php from this as of course it won't match my last catch all rule
Options +FollowSymLinks -Indexes
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} != "index/.php" // I'm not sure on the syntax here
RewriteRule ^(.*)$ $1.php [L]
RewriteRule ^([^/]*)(.*)$ index.php?category=$1&post=$2 [L,QSA]
Solved by adding additional conditions to the last rule
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^([^\.]+)$ $1.php [L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]*)(.*)$ index.php?category=$1&post=$2 [L,QSA]

RewriteCond of htaccess is not working properly

Below is my .htaccess file:
Options -Indexes
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^api/(.*)$ api/index.php/api/$1 [L]
RewriteRule ^lib/(.*) lib/$1 [QSA,L]
RewriteRule ^applications/(.*) applications/$1 [QSA,L]
RewriteRule ^([^\/]*)/index.php /lib/vt-index.php?clientid=$1 [QSA,L]
RewriteRule ^([^\/]*)/([^?]*) /lib/vt/$2?clientid=$1 [QSA]
I have some issues:
lib and applications are folders, but RewriteCond %{REQUEST_FILENAME} !-d has no effect, I have to write condition for lib and application separately.
The main issue is my api calls are not giving me result. I am calling my api as www.myweb.com/api/User/xyz%40gmail.com which is redirecting to www.myweb.com/api/index.php/api/User/xyz%40gmail.com, which is right. But calls to this are blank.
A RewriteCond only gets applied to the immediately following RewriteRule. Conditions aren't set globally nor get applied to more than one rule. You'll either need to repeat the condition for each rule:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^api/(.*)$ api/index.php/api/$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^lib/(.*) lib/$1 [QSA,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^applications/(.*) applications/$1 [QSA,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^\/]*)/index.php /lib/vt-index.php?clientid=$1 [QSA,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^\/]*)/([^?]*) /lib/vt/$2?clientid=$1 [QSA]
Or you can negate the condition and let it pass through first:
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
RewriteRule ^api/(.*)$ api/index.php/api/$1 [L]
RewriteRule ^lib/(.*) lib/$1 [QSA,L]
RewriteRule ^applications/(.*) applications/$1 [QSA,L]
RewriteRule ^([^\/]*)/index.php /lib/vt-index.php?clientid=$1 [QSA,L]
RewriteRule ^([^\/]*)/([^?]*) /lib/vt/$2?clientid=$1 [QSA]
This way, the opposite of your conditions get applied to a rule that simply says "do nothing, and no more rewriting". In order for any of the bottom 5 rules to get applied, the conditions at the top would not have been met.