如何在PHP中创建个性化的URL?

时间:2014-02-15 00:39:33

标签: php regex apache .htaccess mod-rewrite

我想转换这样的网址:

http://somesocial.com/SomeSocial/success/profile?search_user=exampleuser01

进入如下个性化网址:http://somesocial.com/exampleuser01

我尝试在success/preview/user_preview.php内创建一个包含文件的文件夹  这是profile.php的副本,用$profile_id = $_GET['profile_id']header('Location:'.'success/profile?'.$profile_id);从'search_user'获取参数。将搜索到的用户重定向到原始链接。

将我的.htaccess修改为:

RewriteEngine on 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteCond %{REQUEST_FILENAME}\.php -f 
RewriteRule ^(.*)$ $1.php

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !^success/preview/user_preview?search_user=
RewriteRule ^/?([^/]+)$ success/preview/user_preview?search_user=$1 [L]

这很有效,但每次我尝试从主目录(即Index.php)访问文件时,它都会将我重定向到http://somesocial.com/SomeSocial/success/profile.php?search_user=Index.php

为什么?

1 个答案:

答案 0 :(得分:0)

尝试使用更严格的条件重新排序规则:

RewriteEngine on 
RewriteBase /

RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]

RewriteCond %{REQUEST_FILENAME}\.php -f 
RewriteRule ^(.+?)/?$ $1.php [L]

RewriteRule ^([^/]+)/?$ success/preview/user_preview?search_user=$1 [L,QSA]