dsz1966 2013-07-31 06:22
浏览 92
已采纳

我想使用.htaccess从网址中删除问号&.php扩展名

I have a problem with redirect URL in .htaccess. I want to remove .php & question marks from the URL.

For Example: www.example.com/test.php?id=12 to www.example.com/test/12 need like this format.

I tried using this code in my .htaccess

Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /

## don't touch /forum URIs
RewriteRule ^forums/ - [L,NC]

## hide .php extension snippet

# To externally redirect /dir/foo.php to /dir/foo
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R,L]

# To internally forward /dir/foo to /dir/foo.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*?)/?$ $1.php [L]

This code just removes the .php extension from the URL. Also need to remove the question mark.

  • 写回答

2条回答 默认 最新

  • douyun3022 2013-07-31 06:35
    关注

    That code appears to be from one of my answers :)

    Replace your code with this:

    Options +FollowSymLinks -MultiViews
    # Turn mod_rewrite on
    RewriteEngine On
    RewriteBase /
    
    ## don't touch /forum URIs
    RewriteRule ^forums/ - [L,NC]
    
    ## hide .php extension snippet
    
    # To externally redirect /dir/foo.php?id=123 to /dir/foo
    RewriteCond %{THE_REQUEST} ^GET\s([^.]+)\.php\?id=([^&\s]+) [NC]
    RewriteRule ^ %1/%2? [R,L]
    
    # To internally forward /dir/foo/12 to /dir/foo.php?id=12
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME}.php -f
    RewriteRule ^(.+?)/([^/]+)/?$ $1.php?id=$2 [L,QSA]
    
    # To externally redirect /dir/foo.php to /dir/foo
    RewriteCond %{THE_REQUEST} ^GET\s([^.]+)\.php\s [NC]
    RewriteRule ^ %1 [R,L]
    
    # To internally forward /dir/foo to /dir/foo.php
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME}.php -f
    RewriteCond %{QUERY_STRING} ^$
    RewriteRule ^(.*?)/?$ $1.php [L]
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 无源定位系统的时差估计误差标准差
  • ¥15 请问这个代码哪里有问题啊
  • ¥20 python--version在命令端输入结果Python is not defined怎么办?还有pip不是exe格式是不是没安装成功?
  • ¥15 通过GaussianView进行结构微调消除虚频
  • ¥15 调用transformers库
  • ¥15 由于导出的数据名字中带有/,导致Matlab打不开,怎么办?
  • ¥15 新硬盘安装的程序总是崩溃,提示遇到错误
  • ¥15 openpcdet自制数据集评估bev精度和3d精度相同
  • ¥15 excel 上下按钮 显示行
  • ¥20 云卓h12pro 数传问题