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

问题描述:

I want to convert an URL like this:

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

Into a personalized URL like this: http://somesocial.com/exampleuser01

I've tried creating a folder with a file inside success/preview/user_preview.php which is a copy of profile.php to get the parameter from 'search_user' with $profile_id = $_GET['profile_id'] and header('Location:'.'success/profile?'.$profile_id); To redirect the searched user for his original link.

Modified my .htaccess to:

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]

This works perfectly, but every time I try to access a file from the main dir (i.e Index.php) it redirects me to http://somesocial.com/SomeSocial/success/profile.php?search_user=Index.php

Why?

我想转换这样的网址: p>

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

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

我尝试在 success / preview中创建一个包含文件的文件夹 /user_preview.php 这是profile.php的副本,用于从 $ profile_id = $ _GET ['profile_id'] code>和 header获取'search_user'中的参数( '位置:'。'成功/个人资料?'。$ profile_id); code>重定向搜索到的用户的原始链接。 p>

将我的.htaccess修改为: p >

  RewriteEngine on 
RewriteCond%{REQUEST_FILENAME}!-d 
RewriteCond%{REQUEST_FILENAME} \ .php -f 
RewriteRule ^(。*)$ $ 1.php 
 
RewriteEngine On \  nRewriteCond%{REQUEST_FILENAME}!^ success / preview / user_preview?search_user = 
RewriteRule ^ /?([^ /] +)$ success / previe  w / user_preview?search_user = $ 1 [L] 
  code>  pre> 
 
 

这很好用,但每次我尝试从主目录访问一个文件(即Index.php) 它将我重定向到 http://somesocial.com/SomeSocial/success/profile.php?search_user=Index.php code> p>

为什么? p> \ n div>

Try reordering your rule wit more restrictive conditions:

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]