Apache .htaccess:如何删除子文件夹';URL中的子文件夹?
我正在尝试将此URLApache .htaccess:如何删除子文件夹';URL中的子文件夹?,apache,.htaccess,mod-rewrite,redirect,Apache,.htaccess,Mod Rewrite,Redirect,我正在尝试将此URLwww.domain.com/~username重定向到www.domain.com/~username/public,但从URL中删除/public 这是我的.htaccess文件 <IfModule mod_rewrite.c> RewriteEngine on RewriteBase /~username/ RewriteRule !^public/ public%{REQUEST_URI} [L] RewriteCond %{THE_REQUEST}
www.domain.com/~username
重定向到www.domain.com/~username/public
,但从URL中删除/public
这是我的.htaccess
文件
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /~username/
RewriteRule !^public/ public%{REQUEST_URI} [L]
RewriteCond %{THE_REQUEST} ^GET\ /public/
RewriteRule ^public/(.*) /$1 [L,R=301]
</IfModule>
感谢您的帮助 您可以尝试以下代码:
RewriteEngine on
RewriteBase /~username/
RewriteCond %{THE_REQUEST} ^GET\ /public/ [NC]
RewriteRule ^public/(.*)$ $1 [L,R=301,NE]
RewriteRule ^((?!public/).*)$ public/$1 [L,NC]
对这很有效。你能解释一下你是怎么做的吗?这对于将来的参考是非常好的。主要问题是您使用的
%{REQUEST_URI}
包含来自根目录的完整URI,而不仅仅是当前目录中的相对URL,如这里的$1
。
RewriteEngine on
RewriteBase /~username/
RewriteCond %{THE_REQUEST} ^GET\ /public/ [NC]
RewriteRule ^public/(.*)$ $1 [L,R=301,NE]
RewriteRule ^((?!public/).*)$ public/$1 [L,NC]