.htaccess 重写规则仅适用于文章页面

8e2ybdfx  于 2023-03-23  发布在  其他
关注(0)|答案(1)|浏览(87)

我需要帮助弄清楚为什么sitemap生成器拾取了大约64个不存在的URL。根据我的重写规则,我只想重写任何看起来像“/article.php?id=#”到“article/#/article-title-here”的页面。
然而,我在使用站点Map生成器时生成了各种URL,如/article/article. php/index. php或/article/index. php
下面是我的htaccess规则

RewriteEngine On

# Redirect HTTP requests to HTTPS
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.*)$ [NC]
RewriteRule ^(.*)$ https://%1/$1 [R=301,L]

# Redirect old article URLs to new URLs
RewriteCond %{QUERY_STRING} ^id=([0-9]+)$
RewriteRule ^article\.php$ /article/%1/%{QUERY_STRING}? [R=301,L]

# Rewrite article URLs
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^article/([0-9]+)/([^/]+)/?$ article.php?id=$1&title=$2 [L,QSA]

有什么我需要改变,以便它只显示我的5个主要网页和7个文章页?
我已经尝试重写规则多次和回检查与其他网站Map生成器。

ux6nzvsh

ux6nzvsh1#

你的RewriteRule似乎试图做与你的问题所说的相反的事情。
下面的重写规则将把/article.php?id=1234&title=article_title重写为/article/1234/article_title

RewriteEngine On

# Redirect HTTP requests to HTTPS
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.*)$ [NC]
RewriteRule ^(.*)$ https://%1/$1 [R=301,L]

# Rewrite article URLs
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{QUERY_STRING} ^id=([0-9]*)&title=([^/]*)$
RewriteRule ^article\.php$ /article/%1/%2? [L]

相关问题