drtsd7864 2014-02-15 00:39
浏览 35
已采纳

如何在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?

  • 写回答

1条回答 默认 最新

  • dongsimu4422 2014-02-15 11:00
    关注

    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]
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂