查询字符串的友好URL重写规则

时间:2014-09-19 07:39:02

标签: php .htaccess mod-rewrite

我想重写htaccess以接受SEO友好的查询字符串,

我用于通过url传递参数的旧方法是:

http://sitename.com/foldername/edit-agent.php?name=John-Smith

我想将上面的网址更改为:

http://sitename.com/foldername/edit-agent/John-Smith

我的htaccess文件规则如下:

<files page>
ForceType application/x-httpd-php
</files>

<IfModule mod_rewrite.c>

Options +SymLinksIfOwnerMatch
RewriteEngine On
RewriteRule ^edit-agent/([^/\.]+)/?$ edit-agent.php?name=$1 [L]


#resolve .php file for extensionless php urls
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php [L]

</IfModule>

获取传递参数,在edit-agent.php文件中,我使用:

$agent_name = preg_replace('/[^a-zA-Z]/', '', $mysqli->real_escape_string($_GET['name']));

当我进入网址http://sitename.com/foldername/edit-agent/John-Smith时,页面显示内部服务器错误

关于此事的任何线索?

2 个答案:

答案 0 :(得分:0)

试试这个:

Options +FollowSymLinks 
RewriteEngine on  
RewriteRule (.*)/ edit-agent.php?name=$1

答案 1 :(得分:0)

将此代码保留在/foldername/.htaccess

<IfModule mod_rewrite.c>

Options +SymLinksIfOwnerMatch -MultiViews
RewriteEngine On
RewriteBase /foldername/

RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^edit-agent/([^/.]+)/?$ edit-agent.php?name=$1 [L,QSA]

#resolve .php file for extensionless php urls
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php [L]

</IfModule>

RewriteCond %{ENV:REDIRECT_STATUS} ^$将阻止重写规则多次执行。