htaccess and two post parameters

2019-07-17 21:35发布

I developed a small custom MVC framework for my website. it's index.php check $_POST['page'] & $_POST['subpage'] and includes related php file for specified parameter.

Ex: index.php?page=foods (this includes view/foods.php)

Ex: index.php?page=foods&subpage=pizza (this includes view/foods/pizza.php)

Now I want to clean my URLS using .htaccess. I tried few methods to pass second parameter. But I failed. This works fine...

RewriteEngine on
RewriteRule ^([^/\.]+)?$ index.php?page=$1 [L]

But when i tried to pass second parameter its not working fine. This one change css/js/iamges path.

RewriteEngine on
RewriteRule ^([^/\.]+)?$ index.php?page=$1

RewriteRule ^([^/\.]+)/([^/\.]+)?$ index.php?page=$1&sub=$2

3条回答
干净又极端
2楼-- · 2019-07-17 21:53

If you use the QSA (Query String Apend) flag, you can simply pass the entire request string to an index file -

QSA - Appends any query string from the original request URL to any query string created in the rewrite target

So your rule would look similar to this -

RewriteRule %{REQUEST_FILENAME} !-d
RewriteRule %{REQUEST_FILENAME} !-f
RewriteRule ^(.+)$ index.php?uri=$1 [QSA,L]

In your index file you can .explode() by slashes -

$splitArr = explode('/',$_GET['uri']);

And/or use the built in parse_url() function - http://www.php.net/manual/en/function.parse-url.php

parse_url — Parse a URL and return its components

查看更多
叼着烟拽天下
3楼-- · 2019-07-17 22:00

Most url-rewriters of this type explicitly exclude files/directories that actually exist:

RewriteRule %{REQUEST_FILENAME} !-d
RewriteRule %{REQUEST_FILENAME} !-f
查看更多
三岁会撩人
4楼-- · 2019-07-17 22:01
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f [OR]
RewriteRule ^public/(js|css|images)(/.*|$) - [L]
RewriteRule ^(.+)$ index.php?page=$1 [QSA,L]

Here is how finally I've done it !!!

查看更多
登录 后发表回答