apache - .htaccess 在域文件夹中无法正确重定向

标签 apache .htaccess mod-rewrite

需要我的域名重定向

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 检查图像中。

enter image description here
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>

最佳答案

正确重写

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 

关于apache - .htaccess 在域文件夹中无法正确重定向,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52172587/

相关文章:

apache - 使用 .htaccess [#Q1.] 修改 URL

如果文件不存在,Apache 重写,检查另一个目录

linux - Apache 虚拟主机 - 服务器 ip 指向虚拟主机而不是/var/www/html

linux - 如何强制所有请求都来 self 的网站以避免使用 Apache 进行欺骗

php - 在我的 linux 服务器上,我收到一个 php 错误,但我看不到错误,为什么?

apache - 无法解决 htaccess 重写问题(seo 友好的 URL)

python - 将 REST 添加到 Django

.htaccess - 301 使用 get 变量重定向分页

apache - Plesk-Server 上的 Laravel - 只能通过调用/公共(public)访问

wordpress - Apache 301 重定向原始 URL 包含特殊字符的位置