htaccess with multi-parameters

时光怂恿深爱的人放手 提交于 2019-12-24 01:25:30

问题


I'm trying to redirect my links like this:

my href = example.com/?p=users&page=2
my URL should be = example.com/users/page/2

At this moment, my mod_rewrite is working with:

my href = example.com/?p=users
my URL = example.com/users

Here is my htaccess:

<IfModule mod_rewrite.c>
Options +FollowSymLinks
RewriteEngine On

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

回答1:


Have your complete .htaccess as this:

Options +FollowSymLinks
RewriteEngine On

# skip all files and directories from rules below
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]

# skip index.php from any rules below
RewriteRule ^index\.php$ - [L,NC]

# handle /users/page/2 URI
RewriteRule ^([\w-]+)/([\w-]+)/(\d+)/?$ index.php?p=$1&$2=$3 [L,QSA]

# handle /users URI
RewriteRule ^([\w-]+)/?$ index.php?p=$1 [L,QSA]



回答2:


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

Try changing your RewriteRule to:

RewriteRule ^(.+?)(?:/page/(\d*))?$ index.php?p=$1&page=$2 [L,QSA]

A slight caveat of this "one rule" approach is that a request for example.com/users will be rewritten to index.php?p=users&page=, ie. an empty page URL param.

(.+?) - The question mark in this subpattern makes the regex non-greedy, otherwise the optional second part of the regex will always be omitted.



来源:https://stackoverflow.com/questions/39516329/htaccess-with-multi-parameters

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!