Excluding filename using RewriteRule - apache

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]

Related

Infinite redirect after htaccess

RewriteEngine On
RewriteBase /worksheet/
# \?\S matches at least one character after ?
RewriteCond %{THE_REQUEST} \s/(worksheet/rebus)/\?\S [NC]
RewriteRule ^ /%1/? [R=301,L]
RewriteRule ^rebus/?$ /worksheet/rebus/? [L,R=301,NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^/]+)?$ index.php?url=$2&tableName=$1 [L,QSA]
RewriteRule ^([^/]+)/([^/]+)/([^/]+)?$ index.php?url=$2&tableName=$1 [L,QSA]
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/([^/]+)?$ index.php?url=$2&tableName=$1&showSol=$3 [L,QSA]
I changed it for
xyz.com/worksheet/rebus/?random=testing11
to be forwarded for
xyz.com/worksheet/rebus/
buts its endup in infinite redirect.
With your shown samples, please try following htaccess Rules file. Please make sure you keep your htaccess Rules file along with worksheet folder NOT inside worksheet folder.
Clear your browser cache before testing your URLs.
RewriteEngine On
RewriteBase /worksheet/
# \?\S matches at least one character after ?
RewriteCond %{THE_REQUEST} \s/(worksheet/rebus)/\?\S [NC]
RewriteRule ^ /%1/? [R=301,L]
RewriteRule ^rebus/?$ /worksheet/rebus/? [L,R=301,NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/?$ index.php?url=$2&tableName=$1&showSol=$3 [L,QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^/]+)/?$ index.php?url=$2&tableName=$1 [L,QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/?$ index.php?tableName=$1 [L,QSA]

htaccess rewriterule challenge

I'm building a MVC based CMS.
I'm trying to route all the urls as followed:
site.com/admin/... > admin.php?url=...
site.com/member/... > member.php?url=...
site.com/... > index.php?url=...
The first two are no problem
Options -MultiViews
RewriteEngine On
RewriteBase /cms/public_html/
RewriteCond %{REQuEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^admin/(.*)$ admin.php?url=$1 [QSA,L]
RewriteRule ^member/(.*)$ member.php?url=$1 [QSA,L]
But when I want to add the third, that's where I get stuck. I've tried
RewriteRule (.+)$ index.php?url=$1 [QSA,L]
This however 'disables' the first two rules.
Then I tried:
RewriteRule !^admin(.+)$ index.php?url=$1 [QSA,L]
This works for the admin part, also the index is shown, however isn't the rest of the URL parsed in the ?url.
How can I get this to work?
I should also be able to add more site parts (such as site.com/gallery/).
How about another RewriteCond?
Note that you need to list the set of RewriteCond for each RewriteRule as they only apply once.
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^admin/(.*)$ admin.php?url=$1 [QSA,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^member/(.*)$ member.php?url=$1 [QSA,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !^/admin(/|$)
RewriteCond %{REQUEST_URI} !^/member(/|$)
RewriteRule ^(.+)$ index.php?url=$1 [QSA,L]
Demo for the last set here: http://htaccess.mwl.be?share=e7bf5c48-3332-56c1-a83b-d828b3c043a4

Multiple RewriteRules for a single set of RewriteConds

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.

Apache .htaccess to hide both .php and .html extentions

What lines should I add to remove both .html and .php extensions from my site?
I use this for just the .html part:
RewriteEngine on
RewriteBase /
RewriteCond %{http://jobler.ro/} !(\.[^./]+)$
RewriteCond %{REQUEST_fileNAME} !-d
RewriteCond %{REQUEST_fileNAME} !-f
RewriteRule (.*) /$1.html [L]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^.]+)\.html\ HTTP
RewriteRule ^([^.]+)\.html$ http://jobler.ro/$1 [R=301,L]
but I need for both file type extensions to be hidden from the same domain.
I know its late to answer but I giving it since it can be useful to someone :)
#Remove php extension
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
#Remove html extension
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^(.*)$ $1.html
This will remove both php and html extension from your files and works perfectly.
You would want to test whether the file with .html or .php exists, and then redirect to such file if so.
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^(.*)$ $1.html [L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)$ $1.php [L]
RewriteCond %{REQUEST_URI} \.((html)|(php))$
RewriteRule ^(.*)\.([^.]+)$ $1 [R,L]

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.