MVC htaccess rewrite

23,056

Solution 1

Please check these configuration directives if your .htaccess hidden file is in the main root:

RewriteEngine on

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^index\.php/([a-zA-Z0-9_-]+)/method/param$ /index.php?url=$1 [QSA,L]

It will rewrite www.example.com/index.php/$var/method/param into www.example.com/index.php?url=$var but make sure that your .htaccess file is in the main root.

Solution 2

<IfModule mod_rewrite.c>    
    Options +FollowSymLinks
    RewriteEngine On

    RewriteCond %{REQUEST_URI} !-f
    RewriteCond %{REQUEST_URI} !-d
    RewriteCond %{REQUEST_URI} !-l
    RewriteRule ^(.*)$ index.php?url=$1 [QSA,L]
</IfModule>

This one should works

http://www.mysite.com/stats

=>

http://www.mysite.com/index.php?url=stats

Solution 3

Try this one:

<IfModule mod_rewrite.c>
    Options +FollowSymLinks
    RewriteEngine On

    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ index.php?url=$1 [L]
</IfModule>

Solution 4

You have an error on your last line:

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

It should be:

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

In other words change the $l (the letter el) to a $1 (the number one). Also, you probably want your $ after the parentheses to close the line.

Share:
23,056
Ivan
Author by

Ivan

PHP Developer

Updated on April 13, 2020

Comments

  • Ivan
    Ivan about 4 years

    Hello i have problem with my htaccess configuration in my own mvc. IDK what i do wrong? All time i have this message 500:

    Internal Server Error

    The server encountered an internal error or misconfiguration and was unable to complete your request.

    Please contact the server administrator, admin@localhost and inform them of the time the error occurred, and anything you might have done that may have caused the error.

    More information about this error may be available in the server error log.

    I want to make url rewrite to index. Try to do somthing like this

    www.example.com/index.php/controller/method/param

    www.example.com/index.php?url=controler

    My .htaccess look like this:

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

    What i do wrong ?? I read http://httpd.apache.org/docs/current/rewrite/flags.html and do how is there explained.

  • Ivan
    Ivan about 11 years
    okay this work but look. when i put in index.php test var : $url = $_GET['url']; echo $url; this only work on index.php?url=test . When i try index.php/test/other he give me " Notice: Undefined index $url". Whay
  • Anthony Rutledge
    Anthony Rutledge over 7 years
    How is there going to be an underscore, when that is not a valid URL character? He might be better off doing somethign like (.+), instead of trying to specify a specific class of characters.
  • Anthony Rutledge
    Anthony Rutledge over 7 years
    Note %{REQUEST_URI} !-l is for Apache 2.2. Use %{REQUEST_URI} !-L in Apache 2.4.
  • Anthony Rutledge
    Anthony Rutledge over 7 years
    I know it is common, but I wonder why url is used so frequently in the substitution, when in fact $1 is not a URL, but a path component.