首页 > 解决方案 > 当我想在 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]

标签: .htaccesslocalhost

解决方案


你应该这样写:

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.


推荐阅读