Apache .htaccess 如何将 "example.com/dir/webpage.html" 作为 "example.com/webpage"(隐藏目录名称)
Apache .htaccess how to serve "example.com/dir/webpage.html" as "example.com/webpage" (hide directory name)
我在 linux 机器上有一个共享主机计划,我上传了我的静态网站,文件结构如下:
public_html/
--index.html
--style.css
--HTML/
--webpage.html
--CSS/
--JS/
--MEDIA/
现在我希望访问者只需在地址栏中输入:example.com/webpage
并能够访问 example.com/HTML/webpage.html
.
中实际存储的内容
我阅读了有关 Apache mod_rewrite 的文档以及此处关于 SO 和其他主题的许多主题,但我不明白为什么我的 .htaccess 规则不起作用。我确定我没有正确理解它是如何工作的路由和重定向 url,所以我可能犯了一些我看不到的错误。
这是我最后一次尝试 example.com/dir/webpage.html
作为 example.com/webpage
:
RewriteCond %{DOCUMENT_ROOT}/ !-f
RewriteCond %{HTTP_HOST} ^(example.com)$
RewriteRule ^/?(HTML)/(.+)\-?(.+)?(.*)?$ %1/ [PT]
在这里,我只是尝试将 introduction page of mod_rewrite:
中可用的示例应用到我的案例中
我也尝试了很多其他的东西,但没有任何效果。
话虽如此,其他事情我自己也能做到,但尤其是这件事,我实在想不通。如果有人能帮助我,我将不胜感激。
这是我的 .htaccess 文件,其中的注释解释了我在做什么,一切正常。
# redirect all www and http traffic to https not-www
<If "%{HTTP_HOST} == 'www.example.com' || %{REQUEST_SCHEME} != 'https'">
Redirect permanent "/" "https://example.com"
</If>
# defining cache settings for static assets
<ifModule mod_expires.c>
ExpiresActive On
ExpiresDefault M29030400
<FilesMatch "\.(webp|mp4|jpg|jpeg|png|svg+xml|webm|pdf)$">
ExpiresDefault M29030400
Header append Cache-Control "public"
</FilesMatch>
ExpiresByType text/html M3600
ExpiresByType text/css M3600
ExpiresByType text/javascript M3600
ExpiresByType text/plain M3600
</ifModule>
# what to show when resource is missing
ErrorDocument 404 /HTML/404.html
# block requests to files :
<Files ".user.ini">
Require all denied
</Files>
<Files ".htaccess">
Require all denied
</Files>
<Files "php.ini">
Require all denied
</Files>
<Files "error_log">
Require all denied
</Files>
# enable option to rewrite URLs and display them to something nicer
Options +FollowSymLinks -MultiViews
#Start the engine rewriting
RewriteEngine on
# Prevent hotlinking
RewriteCond %{HTTP_REFERER} !^https?://(www\.)?example\.com/?(.*)?$ [NC]
RewriteRule .*\.(jpg|jpeg|gif|png|bmp|mp4|webp|svg)$ - [F,NC]
# redirect permanently example.com/index.html to example.com
RewriteRule ^index(.*)?$ / [L,R=301]
# the following is code I found on SO to strip html tags
# Remove .html (or htm) from visible URL (permanent redirect)
RewriteCond %{REQUEST_URI} ^/(.+)\.html?$ [NC]
RewriteRule - /%1 [L,R=301]
# Quietly point back to the HTML file (temporary/undefined redirect):
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule - %{REQUEST_URI}.html [END]
所以,我实际上能够自己解决这个问题。我将留下最后一段代码与上面的 .htaccess 集成:
RewriteCond %{REQUEST_URI} !^/dir/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ /dir/.html
其中 dir
是要隐藏的目录的名称。
编辑:
另外,这段代码在这种情况下没有用,可以删除:
# the following is code I found on SO to strip html tags
# Remove .html (or htm) from visible URL (permanent redirect)
RewriteCond %{REQUEST_URI} ^/(.+)\.html?$ [NC]
RewriteRule - /%1 [L,R=301]
# Quietly point back to the HTML file (temporary/undefined redirect):
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule - %{REQUEST_URI}.html [END]
希望它对其他人也有一定的用处。
我在 linux 机器上有一个共享主机计划,我上传了我的静态网站,文件结构如下:
public_html/
--index.html
--style.css
--HTML/
--webpage.html
--CSS/
--JS/
--MEDIA/
现在我希望访问者只需在地址栏中输入:example.com/webpage
并能够访问 example.com/HTML/webpage.html
.
我阅读了有关 Apache mod_rewrite 的文档以及此处关于 SO 和其他主题的许多主题,但我不明白为什么我的 .htaccess 规则不起作用。我确定我没有正确理解它是如何工作的路由和重定向 url,所以我可能犯了一些我看不到的错误。
这是我最后一次尝试 example.com/dir/webpage.html
作为 example.com/webpage
:
RewriteCond %{DOCUMENT_ROOT}/ !-f
RewriteCond %{HTTP_HOST} ^(example.com)$
RewriteRule ^/?(HTML)/(.+)\-?(.+)?(.*)?$ %1/ [PT]
在这里,我只是尝试将 introduction page of mod_rewrite:
中可用的示例应用到我的案例中我也尝试了很多其他的东西,但没有任何效果。 话虽如此,其他事情我自己也能做到,但尤其是这件事,我实在想不通。如果有人能帮助我,我将不胜感激。
这是我的 .htaccess 文件,其中的注释解释了我在做什么,一切正常。
# redirect all www and http traffic to https not-www
<If "%{HTTP_HOST} == 'www.example.com' || %{REQUEST_SCHEME} != 'https'">
Redirect permanent "/" "https://example.com"
</If>
# defining cache settings for static assets
<ifModule mod_expires.c>
ExpiresActive On
ExpiresDefault M29030400
<FilesMatch "\.(webp|mp4|jpg|jpeg|png|svg+xml|webm|pdf)$">
ExpiresDefault M29030400
Header append Cache-Control "public"
</FilesMatch>
ExpiresByType text/html M3600
ExpiresByType text/css M3600
ExpiresByType text/javascript M3600
ExpiresByType text/plain M3600
</ifModule>
# what to show when resource is missing
ErrorDocument 404 /HTML/404.html
# block requests to files :
<Files ".user.ini">
Require all denied
</Files>
<Files ".htaccess">
Require all denied
</Files>
<Files "php.ini">
Require all denied
</Files>
<Files "error_log">
Require all denied
</Files>
# enable option to rewrite URLs and display them to something nicer
Options +FollowSymLinks -MultiViews
#Start the engine rewriting
RewriteEngine on
# Prevent hotlinking
RewriteCond %{HTTP_REFERER} !^https?://(www\.)?example\.com/?(.*)?$ [NC]
RewriteRule .*\.(jpg|jpeg|gif|png|bmp|mp4|webp|svg)$ - [F,NC]
# redirect permanently example.com/index.html to example.com
RewriteRule ^index(.*)?$ / [L,R=301]
# the following is code I found on SO to strip html tags
# Remove .html (or htm) from visible URL (permanent redirect)
RewriteCond %{REQUEST_URI} ^/(.+)\.html?$ [NC]
RewriteRule - /%1 [L,R=301]
# Quietly point back to the HTML file (temporary/undefined redirect):
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule - %{REQUEST_URI}.html [END]
所以,我实际上能够自己解决这个问题。我将留下最后一段代码与上面的 .htaccess 集成:
RewriteCond %{REQUEST_URI} !^/dir/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ /dir/.html
其中 dir
是要隐藏的目录的名称。
编辑: 另外,这段代码在这种情况下没有用,可以删除:
# the following is code I found on SO to strip html tags
# Remove .html (or htm) from visible URL (permanent redirect)
RewriteCond %{REQUEST_URI} ^/(.+)\.html?$ [NC]
RewriteRule - /%1 [L,R=301]
# Quietly point back to the HTML file (temporary/undefined redirect):
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule - %{REQUEST_URI}.html [END]
希望它对其他人也有一定的用处。