-1

This is my current .htaccess file:

RewriteEngine on

# remove trailing slash
RewriteRule (.*)(/|\\)$ $1 [R]

# everything
RewriteRule ^(.*?)$ /handler.php?url=$1 [L,QSA]

However, this doesn't work, it throws a 500 Internal Server Error
My previous .htaccess file looked like this:

RewriteEngine on

# remove trailing slash
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule (.*)(/|\\)$ $1 [R]

# everything
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*?)$ /handler.php?url=$1 [L,QSA]

And it worked, except for specific files. However, now I'd like the specific files to redirect into the handler as well. Is there a way to use RewriteRules without the RewriteConds?

Chiri Vulpes
  • 478
  • 4
  • 18
  • With the first version you are getting “an error”, because it causes a redirect loop – `handler.php` itself matches `^(.*?)$`, and so your request gets rewritten over and over and over again. What you want is a `RewriteCond` that only checks if the REQUEST_FILENAME is not `handler.php`. – CBroe Jan 24 '15 at 06:50
  • http://stackoverflow.com/questions/19071324/request-exceeded-the-limit-of-10-internal-redirects – Sumurai8 Jan 24 '15 at 06:52
  • That was the problem, thanks, you guys!! I didn't realise that Rewriting loops around to be `.htaccess`'d again. Do you want to write this as the answer or should I? – Chiri Vulpes Jan 24 '15 at 06:54

2 Answers2

0

Without RewriteCond for file check you can tweak your regex like this:

RewriteEngine on
RewriteBase /

# remove trailing slash for non directories
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+?)/$ $1 [R,L]

# every request not for handler.php
RewriteRule ^((?!handler\.php$).*)$ handler.php?url=$1 [L,QSA]
anubhava
  • 761,203
  • 64
  • 569
  • 643
0

With the help of CBroe and Sumurai8, I was able to fix the problem on my own. The problem was not that you can't have RewriteRules without RewriteConds, but that if you rewrite every url into a single file, it will rewrite requests to that specific file to itself once more, creating an infinite loop.

The new .htaccess:

RewriteEngine on

# remove trailing slash
RewriteRule (.*)(/|\\)$ $1 [R,L]

# everything
RewriteCond %{REQUEST_URI} !^/handler.php$
RewriteRule ^(.*?)$ /handler.php?url=$1 [L,QSA]

Relevant resource: Request exceeded the limit of 10 internal redirects

Community
  • 1
  • 1
Chiri Vulpes
  • 478
  • 4
  • 18
  • This doesn't answer the question since title says **With .htaccess, is it possible to not have RewriteCond?** – anubhava Jan 24 '15 at 07:08
  • Moreover answer is **wrong** due to use of `REQUEST_FILENAME` in it. It will still cause 500 error. – anubhava Jan 24 '15 at 07:12
  • It does not work because `RewriteCond %{REQUEST_FILENAME} !^/handler.php$` condition is wrong and will always fail. – anubhava Jan 24 '15 at 07:14
  • I'm literally using it on my site and it is working. – Chiri Vulpes Jan 24 '15 at 07:15
  • I have tested it **exactly** as is and it caused 500. – anubhava Jan 24 '15 at 07:16
  • Maybe I have a strange configuration. I see, it's usually `REQUEST_URI`, isn't it? I'll update the answer to use that instead. – Chiri Vulpes Jan 24 '15 at 07:16
  • Yes with `REQUEST_URI` it will work (I have removed my downvote now). But still it is not an answer to your own question, you asked an answer without `RewriteCond` and that's where my answer will work for you. – anubhava Jan 24 '15 at 07:18