【问题标题】:Remove .php from URL - Specific case从 URL 中删除 .php - 具体情况
【发布时间】:2015-04-12 02:05:59
【问题描述】:

这是我的 .htaccess 文件,到目前为止一切正常,但我无法从文件中删除 .php 扩展名,我从其他答案中尝试的每个代码都抛出了 500 或 404 错误。请告知在哪里以及添加什么。文件夹的结构是 localhost/myfolder/somefile.php

明确一点 - localhost/myfolder/ 是我项目的根目录。

RewriteEngine On     

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ /myfolder/$1/ [L,R=301]

RewriteRule     ^team-news/([0-9]+[/])?$    posts.php?p=$1&cat=Team\ News
RewriteRule     ^product-news/([0-9]+[/])?$    posts.php?   p=$1&cat=Product\ News
RewriteRule     ^member-specials/([0-9]+[/])?$    posts.php?p=$1&cat=Member\ Specials
RewriteRule     ^ambassador-blogs/([0-9]+[/])?$    posts.php?p=$1&cat=Ambassador\ Blogs
RewriteRule     ^user/([0-9]+[/])?$         profile.php?id=$1
RewriteRule     ^browse-all/([0-9]+[/])?$   searchall.php?p=$1
RewriteRule     ^edit/([0-9]+[/])?$         edit.php?id=$1
RewriteRule     ^articles/([0-9]+[/])?$     post.php?id=$1    

【问题讨论】:

  • 不知道你做错了什么,但有一些像this这样的工具可能会帮助你..
  • 您能否说明错误是什么以及哪个 URL 不起作用?
  • 这里发布的代码一切正常,但现在我需要插入一个从文件中删除 .php 的部分,而无论我尝试什么都行不通。

标签: php apache .htaccess mod-rewrite


【解决方案1】:

在您的 301 规则下方添加此规则:

RewriteEngine On
RewriteBase /myfolder/

# To externally redirect /dir/file.php to /dir/file
RewriteCond %{THE_REQUEST} \s/+(.+?)\.php[\s?] [NC]
RewriteRule ^ %1 [R=302,L,NE]

RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI}\.php -f [NC]
RewriteRule ^(.+?)/?$ $1.php [L]

【讨论】:

  • 它给了我 404 错误,例如对于 /myfolder/search.php 路径它转到 /myfolder/search/ 但有 404
  • 仍然没有,现在它在我的其他 url 中添加了 /.php/,并为 php 文件抛出 404 错误。也许我在我的其他规则中做错了什么,把其他一切都搞砸了?
  • 这两条规则对我来说很好用。注释掉你的斜线 301 规则并重新测试。这个 .htaccess 在哪里?在myfolder 内部或更高级别?
  • 当我删除斜线 301 规则并插入这两个时,我得到 404 错误。 .htaccess 位于 myfolder 内 - 它应该作为我项目的根目录。我这样做是因为整个项目将位于域上的单独文件夹中。
【解决方案2】:

您应该使用两个.htaccess 文件。第一个应该进入你的本地主机根目录(将请求重定向到myfolder),第二个应该进入myfolder(匹配你的PHP文件的路由):

.htaccess:

RewriteEngine On     

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ /myfolder/$1/ [L,R=301]

我的文件夹.htaccess:

RewriteEngine On
RewriteBase /myfolder/

RewriteRule ^team-news/([0-9]+[/])?$         posts.php?p=$1&cat=Team\ News [L]
RewriteRule ^product-news/([0-9]+[/])?$      posts.php?p=$1&cat=Product\ News [L]
RewriteRule ^member-specials/([0-9]+[/])?$   posts.php?p=$1&cat=Member\ Specials [L]
RewriteRule ^ambassador-blogs/([0-9]+[/])?$  posts.php?p=$1&cat=Ambassador\ Blogs [L]
RewriteRule ^user/([0-9]+[/])?$              profile.php?id=$1 [L]
RewriteRule ^browse-all/([0-9]+[/])?$        searchall.php?p=$1 [L]
RewriteRule ^edit/([0-9]+[/])?$              edit.php?id=$1 [L]
RewriteRule ^articles/([0-9]+[/])?$          post.php?id=$1 [L]

请注意,我还如何包含 [L] 以阻止它在找到匹配项后执行任何其他操作。

【讨论】:

    【解决方案3】:

    删除扩展,比如说;浏览器中的 php 或 html 会让浏览器在查找源文件时更加费力。如果您需要它,以下内容可能会对您有所帮助:

    更新:

    PHP:

    RewriteEngine on
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME}\.php -f
    RewriteRule ^(.*)/$ /$1.php
    

    HTML:

    RewriteEngine on
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME}\.html -f
    RewriteRule ^(.*)/$ /$1.html
    

    这些将删除您文件中的所有扩展名(php 和 html)。 注意:查看服务器是否启用 mod 重写模块/扩展

    【讨论】:

    • 这给了我无限循环,例如 /myfolder/search.php 路径到 /myfolder/search/.php/.php/.php/ 到无限
    • 我更新了我的答案,看看是否有帮助。不要忘记检查服务器(apache/else)是否使您能够执行操作
    • 我查过了,确实如此。您看到我发布的所有规则都有效,例如 localhost/myfolder/user/6/ 有效。添加尾部斜杠规则也可以。但是无论我插入什么规则来删除 .php 都会引发错误。也许它干扰了我的其他一些规则?
    • 是的,现在是整个文件。
    • 您是否可以访问网络根目录,我的意思是在 localhost,而不是文件夹。 ?
    【解决方案4】:

    此 sn-p 将允许您重写以删除 php 扩展:

    RewriteEngine On
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^([^\.]+)$ $1.php [NC,L]
    

    如果你希望你的 URL 有一个尾随 /,你可以使用这个 sn-p:

    RewriteEngine On
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^([^/]+)/$ $1.php
    RewriteRule ^([^/]+)/([^/]+)/$ /$1/$2.php
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/)$
    RewriteRule (.*)$ /$1/ [R=301,L]
    

    Source

    【讨论】:

      猜你喜欢
      • 2015-06-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-07-24
      • 1970-01-01
      相关资源
      最近更新 更多