使用 .htaccess 的 seo 友好 url 出现错误

Error come with seo friendly url using .htaccess

首先,我在本地主机上编写了我的网站,然后一切正常,所有 URL 工作,但是当我将它上传到云 (GCP) 时,URL 不工作(它表明 URL =33=] 未找到) 以下代码是 .htaccess file in localhost

RewriteEngine On
RewriteBase /blog_site/

RewriteCond %{REQUEST_FILENAME} !-d [NC]
RewriteCond %{REQUEST_FILENAME} !-f [NC]
RewriteRule ^single_post/(.*)$ single_post.php?post_title= 
RewriteRule ^single_post/(.*)/ single_post.php?post_title=

RewriteRule ^category/(.*)$ category.php?category= 
RewriteRule ^category/(.*)/ category.php?category= 

RewriteRule ^author/(.*)$ author.php?author= 
RewriteRule ^author/(.*)/ author.php?author=

RewriteRule ^author/(.*)/(.*)$ author.php?author=&user= 
RewriteRule ^author/(.*)/(.*)/ author.php?author=&user=

RewriteRule ^search/(.*)$ search.php?search= 
RewriteRule ^search/(.*)/ search.php?search= 

RewriteRule ^([^\.]+)$ .php [NC,L]

也使用基本标签

<base href="/blog_site/" target="_self">

在云服务器上.htaccess 文件的代码是

RewriteEngine On
RewriteBase /

RewriteCond %{REQUEST_FILENAME} !-d [NC]
RewriteCond %{REQUEST_FILENAME} !-f [NC]
RewriteRule ^single_post/(.*)$ single_post.php?post_title= 
RewriteRule ^single_post/(.*)/ single_post.php?post_title=

RewriteRule ^category/(.*)$ category.php?category= 
RewriteRule ^category/(.*)/ category.php?category= 

RewriteRule ^author/(.*)$ author.php?author= 
RewriteRule ^author/(.*)/ author.php?author=

RewriteRule ^author/(.*)/(.*)$ author.php?author=&user= 
RewriteRule ^author/(.*)/(.*)/ author.php?author=&user=

RewriteRule ^search/(.*)$ search.php?search= 
RewriteRule ^search/(.*)/ search.php?search= 

RewriteRule ^([^\.]+)$ .php [NC,L]

基本标签是

 <base href="/" target="_self">

GCP 的 VM 实例 link 这样您就可以看到网站的行为 website link

如果我手动输入 url http://your_IP/contact.php 工作。但是如果我使用 http:///your_IP/contact 是行不通的

仅根据您显示的示例,请您尝试一次以下规则。请确保在测试 URL 之前清除浏览器缓存。你的 htaccess 文件中有多个问题,比如条件不存在,多视图(我们后来在评论中知道它是开启的)导致你的代码出现问题。

Options +FollowSymLinks -MultiViews

RewriteEngine On
RewriteBase /

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^single_post/(.*)/?$ single_post.php?post_title= [NC,L]

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^author/([^/]*)/(.*)/?$ author.php?author=&user= [NC,L]

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(category|author|search)/(.*)/?$ .php?= [NC,L]

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)/?$ .php [L]

OP 编辑​​:

请在继续之前检查 .htaccess 是否已启用。如果您不知道是启用还是禁用,则在您的 .htaccess 文件中键入 ----,如果它显示 500 内部错误,那么它是启用还是禁用。然后上网搜索启用试试