【问题标题】:How to remove the parent directory of a subdirectory from the URL (.htaccess)如何从 URL (.htaccess) 中删除子目录的父目录
【发布时间】:2019-10-03 00:48:25
【问题描述】:

我一直在尝试从 URL 中删除子目录的父目录的所有可能方法。

假设这是我的网址:https://example.com/accounts/login。我需要 url 看起来像这样:https://example.com/login,所以没有accounts 目录。

通常当我尝试访问 https://example.com/login 时,我会收到“服务器遇到内部错误或配置错误,无法完成您的请求。” (又名错误 500 - 内部服务器错误)。

我当前的 .htaccess:

RewriteEngine On 
RewriteCond %{SERVER_PORT} 80 
RewriteRule ^(.*)$ https://example.com/$1 [R,L]

RewriteEngine on 
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^(.*)$ $1.html [NC,L]
RewriteEngine on 
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php [NC,L]

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

<IfModule mod_expires.c>
ExpiresActive On
ExpiresByType image/jpg "access 1 year"
ExpiresByType image/jpeg "access 1 year"
ExpiresByType image/gif "access 1 year"
ExpiresByType image/png "access 1 year"
ExpiresByType text/css "access 1 month"
ExpiresByType text/html "access 1 month"
ExpiresByType application/pdf "access 1 month"
ExpiresByType text/x-javascript "access 1 month"
ExpiresByType application/x-shockwave-flash "access 1 month"
ExpiresByType image/x-icon "access 1 year"
ExpiresDefault "access 1 month"
</IfModule>

<IfModule mod_deflate.c>
  AddOutputFilterByType DEFLATE application/javascript
  AddOutputFilterByType DEFLATE application/rss+xml
  AddOutputFilterByType DEFLATE application/vnd.ms-fontobject
  AddOutputFilterByType DEFLATE application/x-font
  AddOutputFilterByType DEFLATE application/x-font-opentype
  AddOutputFilterByType DEFLATE application/x-font-otf
  AddOutputFilterByType DEFLATE application/x-font-truetype
  AddOutputFilterByType DEFLATE application/x-font-ttf
  AddOutputFilterByType DEFLATE application/x-javascript
  AddOutputFilterByType DEFLATE application/xhtml+xml
  AddOutputFilterByType DEFLATE application/xml
  AddOutputFilterByType DEFLATE font/opentype
  AddOutputFilterByType DEFLATE font/otf
  AddOutputFilterByType DEFLATE font/ttf
  AddOutputFilterByType DEFLATE image/svg+xml
  AddOutputFilterByType DEFLATE image/x-icon
  AddOutputFilterByType DEFLATE image/jpg
  AddOutputFilterByType DEFLATE image/jpeg
  AddOutputFilterByType DEFLATE text/css
  AddOutputFilterByType DEFLATE text/html
  AddOutputFilterByType DEFLATE text/javascript
  AddOutputFilterByType DEFLATE text/plain
  AddOutputFilterByType DEFLATE text/xml

  BrowserMatch ^Mozilla/4 gzip-only-text/html
  BrowserMatch ^Mozilla/4\.0[678] no-gzip
  BrowserMatch \bMSIE !no-gzip !gzip-only-text/html
  Header append Vary User-Agent
</IfModule>

Options -Indexes

【问题讨论】:

  • @anubhava 在这里。
  • 听起来像一个经典的内部重定向循环。但是,由于您明确声称您尝试了所有可能的方法来实现您所寻求的目标而没有成功,所以我们无法添加任何内容,是吗?提示:循环很可能是由RewriteRule ^(.*)$ html/$1 与您尝试添加的任何内容(您没有告诉我们)的组合创建的。
  • @LeonKunštek:谢谢,但我没有看到任何将accounts 内部添加到 URL 的规则。
  • @anubhava 因为我删除了所有连接到accounts文件夹的代码
  • @anubhava 都没有用,所以我删除了它。

标签: .htaccess url directory url-rewriting


【解决方案1】:

用这个块替换你所有的重写规则

RewriteEngine On 

RewriteCond %{SERVER_PORT} 80 
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,NE,L]

RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]

RewriteCond %{DOCUMENT_ROOT}/html/$1.php -f
RewriteRule ^(.+?)/?$ html/$1.php [L]

RewriteCond %{DOCUMENT_ROOT}/html/$1.html -f
RewriteRule ^(.+?)/?$ html/$1.html [L]

RewriteCond %{DOCUMENT_ROOT}/accounts/$1.php -f
RewriteRule ^(.+?)/?$ accounts/$1.php [L]

RewriteCond %{DOCUMENT_ROOT}/accounts/$1.html -f
RewriteRule ^(.+?)/?$ accounts/$1.html [L]

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

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

【讨论】:

  • +1 对于代码。我只是避开了accounts 文件夹并将登录文件夹放在根目录中,现在它可以工作了。我会把你的答案标记为“答案”,尽管它不是。
猜你喜欢
  • 2012-06-25
  • 2013-05-18
  • 2020-01-31
  • 1970-01-01
  • 2011-09-24
  • 2012-03-19
  • 2021-06-24
  • 2020-01-09
  • 1970-01-01
相关资源
最近更新 更多