SISYN
SISYN

Reputation: 2269

Can't get mod_rewrite IfModule to work in .htaccess

I'm trying to get my .htaccess file to forward all URLs to a single page with url parameters so that I can handle the page retrievals that way. What I want to happen is this: say the user types in http://mysite.com/users/dan it should forward to the page http://mysite.com/index.php?url=/users/dan.

Similarly, if the user accessed the URL http://mysite.com/random-link it should forward to http://mysite.com/index.php?url=random-link

Here is the code I tried in my .htaccess file, but it just keeps throwing a 500 error at me:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond % (REQUEST_FILENAME) !-f
RewriteRule (.*)$ index.php?url=$1 <QSA,L>
</IfModule>

I UPDATED MY CODE TO THIS AND IT STILL THROWS A 500 ERROR I changed the < and > to [ and ] and I removed the space after the % in the RewriteCond, but it still throws an error.

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule (.*)$ index.php?url=$1 [QSA,L]
</IfModule>

I'm a novice when it comes to .htaccess, so any help would be greatly appreciated, because I don't know what's causing the server to timeout.

Upvotes: 3

Views: 10710

Answers (1)

Phil
Phil

Reputation: 164952

Couple of problems

  1. Rewrite flags go in square-brackets, eg

    [QSA,L]
    
  2. Your RewriteCondition syntax looks incorrect. Try

    RewriteCond %{REQUEST_FILENAME} !-f
    
  3. Just to be on the safe side, anchor your expression to the start of the string

    RewriteRule ^(.*)$ index.php?url=$1 [QSA,L]
    

Lastly, RewriteEngine and subsequent modifiers requires the FileInfo override. Make sure your server config or virtual host <Directory> section for your document root has

AllowOverride FileInfo

Update

Here's a typical rewrite scheme from an MVC project. This will ignore real files, directories and symlinks

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^(.*)$ index.php?url=$1 [QSA,L]

Upvotes: 5

Related Questions