【问题标题】:PHP rewrite url in htaccess have to point other urlhtaccess中的PHP重写url必须指向其他url
【发布时间】:2016-07-06 06:22:31
【问题描述】:

我想用 .htaccess rewrite 重写一些 url

我有这样的网址:

domain.com/index?/pid=10550

并希望将用户重写为 domain.com/index.php?pid=10550

那么,用户将在后端访问该 url 会发生什么

我是php新手,读过一些这样的博客,运气不好

我也试过了

Options -Multiviews
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^domain.com/index?/$ /domain.com/index.php? [L]

它说页面没有正确重定向 有时会显示一个目录

【问题讨论】:

    标签: php .htaccess url url-rewriting


    【解决方案1】:

    您无法匹配 RewriteRule 模式中的查询字符串。您必须使用 RewriteCond 匹配 {THE_REQUEST} 变量(客户端发送到服务器的完整请求行)

    RewriteEngine on
    #Remove .php
    RewriteCond %{REQUEST_FILENAME}.php -f
    RewriteRule ^(.*?)/?$ /$1.php [L]
    #redirect /index.php/?pid=123 to /index.php?pid=123 
    RewriteCond %{THE_REQUEST} /index\.php/\?pid=([^\s]+) [NC]
    RewriteRule ^ /index?pid=%1 [L,R]
    

    这会将/index.php/?pid=123 重定向到/index.php?pid=123

    【讨论】:

    • 是否可以删除该网址中的 .php 扩展名?
    • 请具体说明。是否要从请求的 uri 中删除 .php?
    • 是的。我还需要删除 .php 扩展名 RewriteRule ^ /index.php?pid=%1 [L,R]
    • 例如:RewriteRule ^ /index?pid=%1 [L,R]
    • 我收到此错误页面未正确重定向
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-12-03
    • 1970-01-01
    • 1970-01-01
    • 2014-09-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多