dongli5785 2014-04-06 14:15
浏览 80
已采纳

使用.htaccess重写URL以进行登录和配置文件系统

I have a website where the url is localhost/project/profile.php?user=usernameand I am trying to get the url to look like this: localhost/project/username

The most I am able to do is get rid of the .php by using the following code:

Options +FollowSymLinks -MultiViews

RewriteEngine On
RewriteBase /

RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R,L,NC]

RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^ %{REQUEST_URI}.php [L]

but that is not what I need right now. Here is the code that is meant to be changing the url - I got it off of another thread.

Options FollowSymLinks
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?user=$1 [L,QSA]

but it obviously doesn't work.

I need to be able to go to the url: localhost/project/username and it registers as the original URL localhost/project/profile.php?user=username

** THE ANSWER **

Thanks to Howlin

RewriteEngine On

RewriteCond %{THE_REQUEST} ^(GET|POST)\ /project/profile\.php\?user=(.*)\ HTTP
RewriteRule ^ /project/%2\? [R=301,L]

RewriteCond %{QUERY_STRING} !user=
RewriteRule ^(.*)$ /project/profile.php?user=$1 [L]
  • 写回答

2条回答 默认 最新

  • doudou8081 2014-04-06 14:49
    关注

    This should work:

    RewriteEngine On
    RewriteCond %{THE_REQUEST} ^(GET|POST)\ /project/profile\.php\?user=(.*)\ HTTP
    RewriteRule ^ /project/%2\? [R=301,L]
    
    RewriteCond %{QUERY_STRING} !user=
    RewriteRule ^project/(.*)$ /project/profile.php?user=$1 [L]
    

    The above will change project/profile.php?user=username to project/username and it will load the information from the project/profile.php?user=username page

    EDIT:

    RewriteEngine On
    
    RewriteCond %{THE_REQUEST} ^(GET|POST)\ /project/profile\.php\?user=(.*)\ HTTP
    RewriteRule ^ /project/%2\? [R=301,L]
    
    RewriteCond %{QUERY_STRING} !user=
    RewriteCond %{REQUEST_FILENAME} !-d  
    RewriteCond %{REQUEST_FILENAME} !-f  
    RewriteRule ^(.*)$ /project/profile.php?user=$1 [L]
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 高价求中通快递查询接口
  • ¥15 解决一个加好友限制问题 或者有好的方案
  • ¥15 关于#java#的问题,请各位专家解答!
  • ¥15 急matlab编程仿真二阶震荡系统
  • ¥20 TEC-9的数据通路实验
  • ¥15 ue5 .3之前好好的现在只要是激活关卡就会崩溃
  • ¥50 MATLAB实现圆柱体容器内球形颗粒堆积
  • ¥15 python如何将动态的多个子列表,拼接后进行集合的交集
  • ¥20 vitis-ai量化基于pytorch框架下的yolov5模型
  • ¥15 如何实现H5在QQ平台上的二次分享卡片效果?