【问题标题】:Hide file extension (.php) in .htaccess file在 .htaccess 文件中隐藏文件扩展名 (.php)
【发布时间】:2013-04-16 21:47:54
【问题描述】:

我知道有很多话题。我尝试了很多次,但它不起作用。

我想要什么?

用户只能看到example.com/en/file,而不是example.com/en/file.php

我的.htaccess file

DirectoryIndex index.php index.html

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


ErrorDocument 404 /index.php

RewriteCond %{HTTP_HOST} ^example\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.example\.com$
RewriteRule ^ru\/index\.php$ "http\:\/\/example\.com\/ru\/" [R=301,L]

RewriteCond %{HTTP_HOST} ^example\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.example\.com$
RewriteRule ^en\/index\.php$ "http\:\/\/example\.com\/en\/" [R=301,L]

等等。每种语言

  1. 我必须添加什么来隐藏扩展?

  2. 它工作后,我应该使用页面之间的链接作为“file.php”还是只使用“file”?

【问题讨论】:

  • 检查RewriteRule 语法 - 正好相反。第一个是“改写什么”,第二个是“改写的结果”
  • 这一定是我在 SO 上看到的被问得最多的问题,在发布之前执行搜索并没有什么坏处
  • 这就像黑客,你总是最好使用一个简单的 PHP 框架。如果您想试一试,请尝试 CodeIgniterCakePHP :)

标签: php


【解决方案1】:

也许是这样的。 根据您认为合适的方式进行修改以进行更具体的模式匹配。

RewriteRule ^(?:(.*)/)?(.*)$ $2.php?lang=$1

重写:

/en/fun => /fun.php?lang=en

/ru/fun => /fun.php?lang=ru

/fun => /fun.php?lang=

【讨论】:

  • 语言还可以。我更喜欢文件夹可见
  • @ЖеняТест 抱歉,不确定您的意思。
【解决方案2】:
DirectoryIndex index.php index.html    

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$      $1.php     [L,QSA]

在您的链接中,不要使用扩展名。

【讨论】:

  • 添加:RewriteRule 上的 RewriteEngine ^(.+)$ $1.php [L] //不起作用:在此服务器上找不到请求的 URL /q_insert_city_image。此外,在尝试使用 ErrorDocument 处理请求时遇到 500 Internal Server Error 错误。
  • 未找到 在此服务器上未找到请求的 URL /en/things_to_visit。我不明白为什么它不起作用 =(
  • 做了一个小测试,它奏效了。确保您可以使用mod_rewrite,并且/en/things_to_visit.php 确实存在。
  • hmmmm,可能是它没有启用.. 我在这里看不到:incinqueterre.com/1.php 在 phpinfo。我以为“url_rewriter.tags”就够了……明天再想,现在我得走了
  • 但是使用“hide index.php”它可以工作,所以它启用了......:/ ohhhh
【解决方案3】:

通过添加一行来修复:

RewriteEngine on
**Options -Multiviews**
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$      $1.php     [L,QSA]

【讨论】:

    猜你喜欢
    • 2014-03-29
    • 2017-10-06
    • 2014-03-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-14
    • 1970-01-01
    • 2012-03-06
    相关资源
    最近更新 更多