Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/apache/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/.htaccess/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/image-processing/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Apache .htaccess在目录中重写-隐藏PHP扩展名并强制尾部斜杠_Apache_.htaccess_Mod Rewrite - Fatal编程技术网

Apache .htaccess在目录中重写-隐藏PHP扩展名并强制尾部斜杠

Apache .htaccess在目录中重写-隐藏PHP扩展名并强制尾部斜杠,apache,.htaccess,mod-rewrite,Apache,.htaccess,Mod Rewrite,我试图从我的文件中隐藏.php扩展名,并在生成的URL上强制使用尾部斜杠 示例:对/about.php的请求将变成/about/,对/about的请求将转到/about/ 当我在主机域的根目录下时,以下重写代码工作正常: RewriteEngine On RewriteRule ^(.*)/$ /$1.php [L] RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_URI} !(.*)/$ RewriteRule ^(.*)$

我试图从我的文件中隐藏.php扩展名,并在生成的URL上强制使用尾部斜杠

示例:对/about.php的请求将变成/about/,对/about的请求将转到/about/

当我在主机域的根目录下时,以下重写代码工作正常:

RewriteEngine On
RewriteRule ^(.*)/$ /$1.php [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ http://edit.mydomain.org/$1/ [R=301,L]

RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /(.*)\.php\ HTTP/ [NC]
RewriteRule .+ http://edit.mydomain.org/%1  [R=301,QSA]
但是,我需要将文件移动到该主机名的目录中。将目录名添加到规则中并在目录中使用.htaccess本身根本不起作用,而且似乎会导致无休止的重定向

我环顾了StackOverflow和其他网站,尝试了许多例子,最后发现了许多不同的错误,其中最常见的是:

  • 一切都是无止境的重定向
  • 除了目录主页以外的所有内容都是500错误
  • about.php重定向到/about,但没有重定向到/about/和/about/显示500错误
  • 一切都正常,但在没有文件名的情况下访问(目录的)index.php主页时,会进入无休止的重定向
  • 事情重定向到edit.mydomain.org/home/username/public_html/mydomain.org/edit/pagename.php,这显然是不存在的
谢谢你的帮助!我确实需要将这些文件保存在一个目录中,尽管.htaccess可以根据需要进入主机名根目录


此文件的目录为edit.mydomain.org/dave/

将其另存为.htaccess并将其放入“dave”目录中

RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !\..+$
RewriteCond %{REQUEST_URI} !/$
RewriteRule ^(.*)$ http://edit.mydomain.org/dave/$1/ [R=301,L]

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

将此保存为.htaccess并将其放入“dave”目录中

RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !\..+$
RewriteCond %{REQUEST_URI} !/$
RewriteRule ^(.*)$ http://edit.mydomain.org/dave/$1/ [R=301,L]

RewriteRule ^(.*)/$ $1.php [L]
这对我有用

RewriteBase /
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^(.*)$ $1.html
这对我有用

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

非常感谢!这非常有效。我还想知道,当有人直接访问索引文件时,如何隐藏index.php?现在,它只是重定向到/index/并显示主页。我为index.php添加了一个重定向行,但现在它给了我一个Apache“This Document has Moved”页面,尽管链接仍然指向index.php。@Brian-如果你不想让人们访问index.php,然后通过将其添加到
RewriteEnginx之后的.htaccess中来更改目录索引,该行位于:
DirectoryIndex.php index.php
。然后,当您将浏览器指向某个文件夹时,Apache将首先查找whatever.php文件,如果该文件存在,它将使用该文件,如果该文件不存在,它将查找index.php。因此,您可以将您的索引文件更改为which.php。任何访问index.php的人都会得到一个404 file not found错误。哦,顺便说一句,如果您真的不想让别人知道您正在使用php,请确保在php.ini中将expose_php设置设置为Off。请参阅此链接:再次感谢!很抱歉问了这么多问题,但是当人们访问edit.mydomain.org/dave目录时,如果没有后面的斜杠,它会被重定向到edit.mydomain.org/dave//home/username/public_html/mydomain.org/edit/hcj/并且因为这是一个服务器路径,这是一个404。@Brian-在根目录的.htaccess
RewriteRule^dave$中重写条件之前尝试添加这个http://edit.mydomain.org/dave/ [R=301,L]
非常感谢!这非常有效。我还想知道,当有人直接访问索引文件时,如何隐藏index.php?现在,它只是重定向到/index/并显示主页。我为index.php添加了一个重定向行,但现在它给了我一个Apache“This Document has Moved”页面,尽管链接仍然指向index.php。@Brian-如果你不想让人们访问index.php,然后通过将其添加到
RewriteEnginx之后的.htaccess中来更改目录索引,该行位于:
DirectoryIndex.php index.php
。然后,当您将浏览器指向某个文件夹时,Apache将首先查找whatever.php文件,如果该文件存在,它将使用该文件,如果该文件不存在,它将查找index.php。因此,您可以将您的索引文件更改为which.php。任何访问index.php的人都会得到一个404 file not found错误。哦,顺便说一句,如果您真的不想让别人知道您正在使用php,请确保在php.ini中将expose_php设置设置为Off。请参阅此链接:再次感谢!很抱歉问了这么多问题,但是当人们访问edit.mydomain.org/dave目录时,如果没有后面的斜杠,它会被重定向到edit.mydomain.org/dave//home/username/public_html/mydomain.org/edit/hcj/并且因为这是一个服务器路径,这是一个404。@Brian-在根目录的.htaccess
RewriteRule^dave$中重写条件之前尝试添加这个http://edit.mydomain.org/dave/ [R=301,L]
棒极了,我想我永远也找不到它了。:)太棒了,我想我永远也找不到了