.htaccess - 当我想在 Localhost 上工作时,Htaccess 指向在线
问题描述
我将本地服务器 Apache 更改为 XAMPP。之后,我的项目已经开始访问在线网站 yusufcode.com(我的网站链接),而我想在 localhost 上工作。问题出在哪里?这是第一个问题。
我的第二个问题是我的链接在本地主机上没有 .php 扩展名时不起作用。
访问:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)$ $1.php
RewriteRule ^([a-z]+)\/?$ $1.php [NC]
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://www.yusufcode.com/$1 [R,L]
解决方案
你应该这样写:
Options -MultiViews
RewriteEngine on
# Redirect to HTTPS (and www) but only on "live" site
RewriteCond %{HTTP_HOST} !^www\. [OR]
RewriteCond %{SERVER_PORT} 80
RewriteCond %{HTTP_HOST} ^(?:www\.)?(yusufcode\.com) [NC]
RewriteRule (.*) https://www.%1/$1 [R=301,L]
# Handle extensionless .php files
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule (.*) $1.php [L]
处理无 PHP 扩展 URL 的原始指令存在许多问题……第二个指令RewriteRule
无条件运行。文件检查可能正在测试所请求的文件不同的文件。
更新:这当然假设.htaccess
在您的本地 Apache 服务器上的服务器配置中启用了覆盖。这不是 Apache 2.4 的默认设置。使用指令.htaccess
启用覆盖。AllowOverride
例如,在您的服务器配置中的某处,您需要这样的东西:
<Directory /path/to/document-root/>
# Allow access
Require all granted
# Allow all htaccess overrides
AllowOverride All
</Directory>
默认值为AllowOverride None
.
推荐阅读
- asp.net - 如何从 .Net 中的字符串验证模型/类的属性?
- python - 循环中的矢量化比 numba jited 函数中的嵌套循环慢
- text-classification - 如何在 Sagemaker 中训练多标签文本分类?
- php - 如果变量存在于数组中,则php中的数组条件
- python - 如何找到库 RAE 的文档或示例?
- python - 有没有办法写一段python代码来查看网页上是否存在一系列单词
- c++ - 函数模板的显式模板特化不起作用
- tensorflow - 如何在 tensorflow.js 中获取/设置监督模型的权重?
- python - python中的文本数据预处理
- ios - 重置 AVPlayer 和 ARImageTracking