简体   繁体   English

.htaccess-将/path.php重写为/ path /,而不是对子目录中的文件进行404处理

[英].htaccess - rewriting /path.php to /path/ and not 404-ing files in sub-directories

Firstly, I apologise. 首先,我道歉。 This topic has been rehashed repeatedly, (I know. I read them.) But for whatever reason, my case is either odd, or I'm breaking something else in the .htaccess. (我知道。我读过它们。)但是,由于某种原因,我的情况很奇怪,或者我在.htaccess中有其他问题。

Objective 目的

  1. Hide PHP file extensions, 隐藏PHP文件扩展名,
  2. 301 any attempt at getting to them to their masquerading path, 301试图使他们走上伪装之路的任何尝试,
  3. Allow PHP files within sub-directories to be actually referenced, 允许子目录中的PHP文件被实际引用,
  4. Rewrite underscores in file names as slashes (/) so they appear to be nested. 将文件名中的下划线重写为斜杠(/),以使它们看起来像是嵌套的。 ( edit: or translate / in the URL to be _'s in the file - unless the directory exists.) 编辑:或将URL中的/转换为文件中的_-除非目录存在。)

... 3 & 4 are the kickers. ... 3&4是踢腿者。

The following code is the .htaccess I've been using prior to this... 以下代码是我在此之前一直使用的.htaccess ...

<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>

<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /

## Imagine Some Code here ##

# Redirect to domain with www.
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule .* http://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
# Same for HTTPS:
RewriteCond %{HTTPS} on
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule .* https://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

# Ensure all directory URLs have a trailing slash.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !\/$
RewriteCond %{REQUEST_URI} !\/[^\/]*\.[^\/]+$
RewriteRule .* http://%{HTTP_HOST}%{REQUEST_URI}/ [L,R=301]
# Same for HTTPS:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !\/$
RewriteCond %{REQUEST_URI} !\/[^\/]*\.[^\/]+$
RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI}/ [L,R=301]
</IfModule>

I have tried adding the following snippets where I noted. 我尝试在我注意到的地方添加以下片段。

RewriteRule    ^$    /index.php    [L]
RewriteRule    ^([a-zA-Z0-9\-\_/]*)/$    /$1/index.php    [L]
RewriteRule    ^([a-zA-Z0-9\-\_/]*)\.(html|htm)$    /$1.php    [L]
RewriteRule    ^([a-zA-Z0-9\-\_/]*)$    /$1.php    [L]

and

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

and the most success was: 最成功的是:

# Unless directory, remove trailing slash
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/$ https://www.actual_domain.com.au/$1 [R=301,L]

# Redirect external .php requests to extensionless url
RewriteCond %{THE_REQUEST} ^(.+)\.php([#?][^\ ]*)?\ HTTP/
RewriteRule ^(.+)\.php$ https://www.actual_domain.com.au/$1 [R=301,L]

# Resolve .php file for extensionless php urls
RewriteRule ^([^/.]+)$ $1.php [L]

That last one was pretty good actually, however, I couldn't access php files at all in any format (with/without extension) in sub-directories. 最后一个实际上非常好,但是,我根本无法在子目录中以任何格式(带/不带扩展名)访问php文件。 And all attempts at adding in something that handled requirement 4 just didn't work. 所有添加满足要求4的东西的尝试都没有用。

As much as I would love for someone to just go BAM, done. 就我所希望的那样,有人去完成BAM。 Use and enjoy. 使用和享受。 I would really like to understand what is going wrong too. 我也很想知道出什么问题了。

Thanks in advance. 提前致谢。

-- edit -- -编辑-

As requested by misorude, the following URLs are examples. 根据misorude的要求,以下URL是示例。

URL: actual_domain.com.au/blog - Reads file: /blog.php 网址: actual_domain.com.au/blog- 读取文件: /blog.php

URL: actual_domain.com.au/path/to-file - Reads file: /path_to-file.php 网址: actual_domain.com.au/path/to-file- 读取文件: /path_to-file.php

URL: actual_domain.com.au/dir/file - Reads file: /dir/file.php 网址: actual_domain.com.au/dir/file- 读取文件: /dir/file.php

With the last example (/dir/) the dir directory exists. 在最后一个示例(/ dir /)中,存在dir目录。 With the /path/ one, it isn't a directory. 使用/ path /一个,它不是目录。

Now, as for my wording for objective 3, I have found that the .htaccess code that I have tried appears to prevent anything physically in a sub-directory returns 404. (case in point, that last example above referencing /dir/) 现在,关于目标3的措辞,我发现我尝试的.htaccess代码似乎在物理上阻止子目录中的任何内容返回404。(例如,上面的最后一个示例引用/ dir /)

And lastly, with respect to the rewriting of / or _, the file contains the _ and I'd like it to present as a / in the url. 最后,关于/或_的重写,该文件包含_,我希望它在URL中显示为/。

-- edit -- -编辑-

I've managed to do this: 我设法做到了:

# external redirect from actual URL to pretty one
RewriteCond %{REQUEST_URI} path_to-file\.php [NC]
RewriteRule ^ /path/to-file [R=302,L,NE]

# internal forward from pretty URL to actual one
RewriteRule ^path/to-file/?$ /path_to-file.php [L,NC]

... which works checking the headers, but 404's in practice. ...可以检查标题,但实际上是404。 It is also hard coding a specific path. 也很难对特定路径进行编码。 Which isn't really what I wanted to do. 这不是我真正想做的。

Now, the failing in practice. 现在,在实践中失败了。 Using: https://www.webconfs.com/http-header-check.php with the url: https://actual_domain.com.au/path/to-file 使用: https : //www.webconfs.com/http-header-check.php和url: https : //actual_domain.com.au/path/to-file

I get this: 我得到这个:

HTTP/1.0 404 Not Found =>
Cache-Control => private, no-cache, no-store, must-revalidate, max-age=0
Pragma => no-cache
Content-Type => text/html
Content-Length => 618
Date => Mon, 12 Aug 2019 06:27:16 GMT
Server => LiteSpeed
Location => https://actual_domain.com.au/path_to-file.php
Alt-Svc => quic=":443"; ma=2592000; v="35,39,43,44"
Connection => close

You can see that the location redirection works, but it still gives 404 - even though going to the redirected path works! 您可以看到位置重定向有效,但仍然提供404-即使转到重定向路径也可以! :( :(

Thoughts? 思考?

You may have your .htaccess as this: 您可能会使用.htaccess文件,如下所示:

# turn directory slash on
DirectorySlash On
RewriteEngine On

# add www to a domain name taking care of both http and https
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTPS}s on(s)|
RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L,NE]

# remove .php from URLs
RewriteCond %{THE_REQUEST} \s/+(.+?)\.php[\s?] [NC]
RewriteRule ^ /%1 [R=301,NE,L]

# ignore all files and directories
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]

# add .php extension
# RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^([^.]+?)/?$ $1.php [L]

# Replace / by _ recursively
RewriteRule ^([^/]+)/(.+)$ $1_$2

Make sure when you test, you only have these rules and you test from a new browser to avoid old cache. 确保在测试时只有这些规则,并且在新的浏览器上进行测试以避免旧的缓存。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM