【问题标题】:url rewriting with .htaccess使用 .htaccess 重写 url
【发布时间】:2011-01-28 17:49:28
【问题描述】:

我发现了一些关于 url 重写的问题: .htaccess: Problem with URL rewriting.htaccess question - URL-rewriting

我尝试了他们的方法,但在我的情况下不起作用? 我的看起来很简单:

http://example.org.uk/member/home.php?profile --> http://example.org.uk/member/home/profile/
http://example.org.uk/member/home.php?history --> http://example.org.uk/member/home/history/

Options +FollowSymLinks
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
#RewriteRule ^home/([0-9][0-9])/$ /home.php?$1
RewriteRule ^/([^/]+).php$ /home.php?$1 [L]

有什么想法吗?谢谢

【问题讨论】:

  • 您的规则和示例相互矛盾。您希望将home.php?profile 映射到home/profile(示例)还是将home/profile 重写为home.php?profile(重写代码)?
  • @Linus:我想映射到主页/配置文件并删除 php ex。

标签: php .htaccess url-rewriting


【解决方案1】:

你让它有点复杂,以下应该可以工作

http://example.org.uk/member/home.php?profile --> http://example.org.uk/member/home/profile/
http://example.org.uk/member/home.php?history --> http://example.org.uk/member/home/history/



RewriteRule ^/member/home/(.*)/ /home.php?$1 [L]

或更一般地

RewriteRule ^/(.*)/(.*)/(.*)/ /home.php?$3 [L]

【讨论】:

  • 第一个参数后面不应该有问号吗?我还在学习 RewriteMagic :)
  • 查看我对stackoverflow.com/questions/559186/php-rewrite-rules/… 的回答,不需要吗?在上面。
  • 谢谢。它有点工作。但我需要将我的网址更改为绝对路径。还有1件事。当我在home/profile/ 上时,对!然后当我点击home/history 上的链接时,网址变成这样home/home/history 知道为什么吗?
  • 这就是 url 的工作方式,如果 url 有一个前导 .. 它将进入一个目录,如果它有一个 / 它将从域基础开始,如果它没有这些它将添加到当前网址。在您的情况下,为了相对 url 的目的,实现当前 url 的重要性是 example.org.uk/member/home/history 而不是 example.org.uk/home.php?history 。测试替代方案,看看它们是如何工作的。
【解决方案2】:

在 RewriteRule 中,您有“^/([^/]+).php$”,但您不希望在 URL 中使用 .php 扩展名。从重写规则中删除 .php。 像这样的东西应该可以工作:

RewriteRule ^/member/home/.*$ /member/home.php?$1 [L]

【讨论】:

    【解决方案3】:
    Options +FollowSymLinks
    RewriteEngine on
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^/member/home/([a-zA-z0-9]+)/$ /member/home.php?$1 [L]
    

    【讨论】:

      【解决方案4】:

      它仍然会寻找“.php”,因此您可以使用

      RewriteRule ^/([^/]*)/$ home.php?$1
      

      【讨论】:

        猜你喜欢
        • 2011-04-03
        • 2014-12-23
        • 2015-01-25
        • 1970-01-01
        相关资源
        最近更新 更多