简体   繁体   中英

PHP (htaccess) rewrite with $_GET request

I made an simple framework for my website development.

I am currently using the this URL pattern:

www.example.com?r=account/login

But I would like to change it to:

www.example.com/account/login

remove r= from the URL.

I have seen YII framework does this. But I couldn't find a way to do this.

Solution: Add the code below to your .htaccess

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteRule ^([a-zA-Z0-9-/]+)$ index.php?r=$1
    RewriteRule ^([a-zA-Z0-9-/]+)/$ index.php?r=$1
</IfModule>`

What you are looking for are SEO friendly URLs which are quite common to use & implement. This is the .htaccess from a basic WordPress install which achieves a similar goal:

# BEGIN WordPress
<IfModule mod_rewrite.c>
  RewriteEngine On
  RewriteBase /
  RewriteRule ^index\.php$ - [L]
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteRule . /index.php [L]
</IfModule>
# END WordPress

Then in index.php you can test this by doing a variable dump of $_GET like so:

echo '<pre>';
print_r($_GET);
echo '</pre>';

I see you found a solution however you don't/shouldn't need two rewrite rules for the same thing just to add the / . You can use a ? to make it optional. I also recommend using [L] on the rewrites so that it stops when the rule is met.

This is more of what you were looking for.

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^([^/]+)/?$ /index.php?r=$1 [L]
</IfModule>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM