【问题标题】:.htaccess not redirecting properly when in domain folder.htaccess 在域文件夹中未正确重定向
【发布时间】:2019-02-09 20:36:42
【问题描述】:

需要我的域来重定向

FROM => mydomain.com/Section

TO => mydomain/index.php?controller=Section

我希望用户如何查看 URL=> mydomain.com/Section

这是我编写的 .htaccess 文件

AddHandler application/x-httpd-php56 .php
DirectoryIndex index.php
RewriteEngine on
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R,L]
# above code does work at least the https redirect but below wont

RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond $1 !^(index\.php|robots\.txt)
RewriteCond %{REQUEST_URI} !(\.$)
RewriteRule /([0-9a-zA-Z-_]+)/? index.php?controller=$1 [NC,QSA,L]
<filesmatch "\.(html|htm|js|css)$"="">
  FileETag None
  <ifmodule mod_headers.c="">
     Header unset ETag
     Header set Cache-Control "max-age=0, no-cache, no-store, must-revalidate"
     Header set Pragma "no-cache"
     Header set Expires "Wed, 11 Jan 1984 05:00:00 GMT"
  </ifmodule>
</filesmatch>

注意:在我的文件系统中,当 .htaccess 位于根目录 (root/.htaccess) 中时,此功能有效,但在域文件夹 (root/mydomain.com/.htaccess) 中时无法完全工作,我需要它在其中工作如果可能的话,域文件夹,因为主机会在域文件夹中自动生成一个 .htaccess 文件,这会弄乱根目录中的 .htaccess。

感谢您的帮助!

更新:2018 年 9 月 6 日

似乎 HostGator 已读取并更新了我的 .htaccess 文件并强制进行目录检查,我没有将此重定向添加到下面的 cpanel 检查图像中。

AddHandler application/x-httpd-php56 .php 
Options -MultiViews 
RewriteBase / 
RewriteEngine On 
#redirect to https 
RewriteCond %{HTTPS} Off 
RewriteRule (.*) https://www.%{HTTP_HOST}%{REQUEST_URI} [R,L] 
#end of https redirect 

#Pretty url code 
RewriteCond %{HTTP_HOST} ^(www\.)?6thsense.co$ 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteCond $1 !^(index\.php|robots\.txt) 
RewriteCond %{REQUEST_URI} !(\.$) 
RewriteCond %{REQUEST_URI} !^/\.well-known/acme-challenge/[0-9a-zA-Z_-]+$ 
RewriteCond %{REQUEST_URI} !^/\.well-known/cpanel-dcv/[0-9a-zA-Z_-]+$ 
RewriteCond %{REQUEST_URI} !^/\.well-known/pki-validation/[A-F0-9]{32}\.txt(?:\ Comodo\ DCV)?$ 
RewriteRule /([0-9a-zA-Z-_]+)/? /index.php?controller=$1 [NC,QSA,L]
#endof pretty url 

#<filesmatch "\.(html|htm|js|css)$"="">
#  FileETag None
#  <ifmodule mod_headers.c="">
#     Header unset ETag
#     Header set Cache-Control "max-age=0, no-cache, no-store, must-revalidate"
#     Header set Pragma "no-cache"
#     Header set Expires "Wed, 11 Jan 1984 05:00:00 GMT"
#  </ifmodule>
#</filesmatch>

【问题讨论】:

  • "因为主机会在域文件夹中自动生成一个 .htaccess 文件,这会弄乱根目录中的 .htaccess" 这听起来像是那里的问题。此自动生成过程是否可以让您配置其他内容?是什么控制了这种自动生成?你能编辑一下吗?
  • 检查我的更新,自动生成无法从我看到的内容中修改,它只允许我添加和删除重定向。
  • Hostgator 的帮助页面没有提及在您完成后会编辑文件的任何内容:support.hostgator.com/articles/specialized-help/technical/… 我想知道是不是 cpanel 搞砸了它?我不是那个的用户。
  • 我修复了它,hostgator 确实改变了它,但这不是重写规则写错的问题。我很烂

标签: apache .htaccess mod-rewrite


【解决方案1】:

你可以使用:

RewriteRule ([0-9a-zA-Z-_]+?)/?$ /index.php?controller=$1 [NC,QSA,L] 

适用于 mydomain.com/Sectionmydomain.com/Section/

【讨论】:

  • 快速提问,这是否适用于以下请求:mydomain.com/Section/?mario=cool to display: $_GET = ['controller' => 'Section' , 'mario' => 'cool'],因为据我了解,$ char 表示 URL 的结尾
【解决方案2】:

正确重写

RewriteRule ([0-9a-zA-Z-_]+) /index.php?controller=$1 [NC,QSA,L] 
#endof pretty url

不正确的重写

RewriteRule /([0-9a-zA-Z-_]+)/? /index.php?controller=$1 [NC,QSA,L]
#endof pretty url 

【讨论】:

  • 在 RewriteRule 中,测试 url 永远不会以 / 开头!
  • 你可以使用RewriteRule ^([0-9a-zA-Z-_]+)/?$ /index.php?controller=$1 [NC,QSA,L]
猜你喜欢
  • 2011-10-14
  • 2020-03-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-11-08
  • 1970-01-01
  • 2022-01-17
相关资源
最近更新 更多