带有两个参数的虚荣/清洁URL

时间:2014-07-20 20:10:29

标签: php .htaccess url mod-rewrite url-rewriting

所以我有一个.htaccess文件,将/ username放入profile.php?id = $ 0

我需要能够接受两个参数,而不会与从php文件中取出.php扩展名的重写规则发生冲突。

以下是我想要的明确示例:

/profile.php?id=username&category=schooling ------> / username / schooling

我不希望这会影响其他php文件,例如 / about

我的.htaccess文件如下:

Options +FollowSymlinks
RewriteEngine on
RewriteRule ^([A-Za-z0-9]+)$ $1.php
RewriteRule ^([A-Za-z0-9\._\-]+)+[^\.php]$ profile.php?id=$0 [NC]

非常感谢任何帮助,谢谢! :)


更新

所以这对我有用,但图像不起作用

Options +FollowSymLinks -MultiViews
RewriteEngine On

RewriteRule ^([A-Za-z0-9]+)$ $1.php [L]
RewriteRule ^([A-Za-z0-9\._\-]+)+[^\.php]$ profile.php?id=$0 [L]
RewriteRule ^([^/]*)/([^/]*)$ /profile.php?id=$1&category=$2 [L]

有什么想法吗?我知道背后的原因,但不知道如何解决它。我认为图像网址也被重定向。

2 个答案:

答案 0 :(得分:0)

试试这个:

Options +FollowSymLinks -MultiViews
RewriteEngine On

# IF we have an existing file or folder
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
# THEN leave the url unchanged, and skip the next two rules
RewriteRule ^ - [L,S=2]
# ELSE (i.e., it's a pretty url that doesn't correspond to a real file or folder)
# Apply these rules if they match
RewriteRule ^(\w+)/?$ profile.php?id=$1 [L,QSA]
RewriteRule ^(\w+)/(\w+)/?$ profile.php?id=$1&category=$2 [L,QSA]

答案 1 :(得分:0)

也许

Options +FollowSymlinks
RewriteEngine on
RewriteRule ^/([A-Za-z0-9\._\-]+)/([A-Za-z0-9]+)$ profile.php?id=$1&category=$2 [L, QSA, NC]
RewriteRule ^([A-Za-z0-9\._\-]+)+[^\.php]$ profile.php?id=$1 [L, QSA, NC]
RewriteRule ^([A-Za-z0-9]+)$ $1.php [L, QSA]

如果匹配L的规则匹配,L标志用于停止处理剩余规则的mod_rewrite表单。