Apache 将下划线替换为斜杠并删除“删除”;。php“;in.htaccess

Apache 将下划线替换为斜杠并删除“删除”;。php“;in.htaccess,apache,.htaccess,mod-rewrite,url-rewriting,Apache,.htaccess,Mod Rewrite,Url Rewriting,我已经在互联网和Stackoverflow上搜索了好几个小时,但我无法完全解决我的问题。我几年前就知道了,但是文件丢了,我再也无法重建了。所以我希望你的帮助 我有HTML URL,我想模拟文件夹,我还想删除扩展名.HTML INPUT: domain.com/file.html OUTPUT: domain.com/file/ INPUT: domain.com/folder1_folder2_file.html OUTPUT: domain.com/folder1/folder2/file/

我已经在互联网和Stackoverflow上搜索了好几个小时,但我无法完全解决我的问题。我几年前就知道了,但是文件丢了,我再也无法重建了。所以我希望你的帮助

我有HTML URL,我想模拟文件夹,我还想删除扩展名
.HTML

INPUT: domain.com/file.html
OUTPUT: domain.com/file/

INPUT: domain.com/folder1_folder2_file.html
OUTPUT: domain.com/folder1/folder2/file/
我也不太确定队伍末尾的旗帜

到目前为止不起作用的代码:

RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ $1 [R=301,L]
RewriteRule ^([^_]*)_(.*)$ $1/$2 [R=301,L]
RewriteRule ^([^_]*)?_([^_]*)?_(.*)$ $1/$2/$3 [R=301,L]

谢谢你的帮助。

你能试试下面的吗

RewriteEngine ON
RewriteCond %{REQUEST_URI} _
RewriteCond %{REQUEST_URI} \.html/?$
RewriteCond %{THE_REQUEST} /([^_]*)_([^_]*)_([^.]*)\.html\s [NC]
RewriteRule ^(.*)$ /%1/%2/%3/ [R=301,NC,L]

RewriteCond %{REQUEST_URI} !_
RewriteCond %{REQUEST_URI} \.html/?$
RewriteCond %{THE_REQUEST} /([^.]*)\.html\s [NC]
RewriteRule ^(.*)$ /%1/ [R=301,NC,L]
使用curl命令进行测试:

curl -IL "http://localhost:80/file.html"
HTTP/1.1 301 Moved Permanently
Server: Apache/2.4.46 (Win64) OpenSSL/1.1.1g PHP/7.4.11
Location: http://localhost/file/

curl -IL "http://localhost:80/folder1_folder2_file.html"
HTTP/1.1 301 Moved Permanently
Server: Apache/2.4.46 (Win64) OpenSSL/1.1.1g PHP/7.4.11
Location: http://localhost/folder1/folder2/file/

你能试试下面的吗

RewriteEngine ON
RewriteCond %{REQUEST_URI} _
RewriteCond %{REQUEST_URI} \.html/?$
RewriteCond %{THE_REQUEST} /([^_]*)_([^_]*)_([^.]*)\.html\s [NC]
RewriteRule ^(.*)$ /%1/%2/%3/ [R=301,NC,L]

RewriteCond %{REQUEST_URI} !_
RewriteCond %{REQUEST_URI} \.html/?$
RewriteCond %{THE_REQUEST} /([^.]*)\.html\s [NC]
RewriteRule ^(.*)$ /%1/ [R=301,NC,L]
使用curl命令进行测试:

curl -IL "http://localhost:80/file.html"
HTTP/1.1 301 Moved Permanently
Server: Apache/2.4.46 (Win64) OpenSSL/1.1.1g PHP/7.4.11
Location: http://localhost/file/

curl -IL "http://localhost:80/folder1_folder2_file.html"
HTTP/1.1 301 Moved Permanently
Server: Apache/2.4.46 (Win64) OpenSSL/1.1.1g PHP/7.4.11
Location: http://localhost/folder1/folder2/file/

我终于自己弄明白了。它用非数字字符和破折号重写URL

对于任何感兴趣的人:

RewriteEngine On
RewriteRule ^/?([a-zA-Z-]+)$ $1.html [N]
RewriteRule ^/?([a-zA-Z-]+)/([a-zA-Z-]+)/([a-zA-Z-]+)$ $1_$2_$3.html [L]

不过,谢谢你的努力。=)@RavinderSingh13

我终于自己弄明白了。它用非数字字符和破折号重写URL

对于任何感兴趣的人:

RewriteEngine On
RewriteRule ^/?([a-zA-Z-]+)$ $1.html [N]
RewriteRule ^/?([a-zA-Z-]+)/([a-zA-Z-]+)/([a-zA-Z-]+)$ $1_$2_$3.html [L]

不过,感谢您的努力=)@RavinderSingh13

我尝试了第一个代码片段。不起作用。404错误。我不知道如何处理第二个。@Mel,如果你正在点击link
http://localhost:80/folder1_folder2_file.html
(你可以输入你的域名),当我使用它时,它对我来说非常好,让我知道吗?我尝试了第一个代码片段。不起作用。404错误。我不知道如何处理第二个。@Mel,如果你正在点击link
http://localhost:80/folder1_folder2_file.html
(您可以输入您的域名),当我使用它时,它对我非常有效,请告诉我?当您现在访问浏览器中的
domain.com/file/
时,它是否打开了
/file.html
?否,它仍然显示未找到的页面。在这种情况下,您将从
/folder1/folder2/file/
内部重写到
/folder1\u folder2\u file.html
当您现在在浏览器中访问
domain.com/file/
时,它是否打开了
/file.html
?否,它仍然显示未找到的页面。在这种情况下,您将从
/folder1/folder2/file/
内部重写为
/folder1\u folder2\u file.html
。感谢您告知您的工作解决方案,对于我的解决方案,您是否遇到任何错误等?这对我来说很好:)嗨,谢谢你让我知道你的工作解决方案,因为我的解决方案,你有什么错误吗?这对我来说很好:)