R = 301 重定向的 RewriteRule

问题描述 投票:0回答:1

在我的 .htaccess 中,我有一个重写规则,如下所示:

RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

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 ^country/([0-9]+)-([a-z_-]+)-(page)-([0-9]+)/?$ nation_notes.html?country=$1&title=$2&page=$3&pagenum=$4  [L,NC]

这允许我的网站采用这样的链接:

https://www.example.com/country/6-Argentina-page-1

并显示来自此的数据:

https://www.example.com/nation_notes.html?country=6&title=Argentina&page=1&pagenum=1

但是,Google 开始对我的页面取消索引,因为它们是“带有重定向的页面”。我希望 RewriteRule 指定其中每一个都是 R=301(301 重定向)。然而,当我添加:

[L,R=301,NC]

然后重定向中断,并开始将我网站的根目录添加到 URL,如下所示:

https://www.example.com/home2/username/public_html/nation_notes.html?country=6&title=Argentina&page=1page&pagenum=1

“home2/username/public_html/”被添加到 URL 的中间。显然这破坏了代码。

如何修复此 RewriteRule 并添加 301 重定向并且仍然让一切正常运行?

我尝试过以下方法:

RewriteRule ^country/([0-9]+)-([a-z_-]+)-(page)-([0-9]+)/?$ nation_notes.html?country=$1&title=$2&page =$3&pagenum=$4 [L,R=301,NC]

RewriteRule ^country/([0-9]+)-([a-z_-]+)-(page)-([0-9]+)/?$ nation_notes.html?country=$1&title=$2&page =$3&pagenum=$4 [L,R=301]

RewriteRule ^country/([0-9]+)-([a-z_-]+)-(page)-([0-9]+)/?$ nation_notes.html?country=$1&title=$2&page =$3&pagenum=$4 [R=301,L,NC]

...以及L、R-301和NC的其他组合。

这可能有用:我的完整 .htaccess 文件看起来像这样:

RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

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 ^country/([0-9]+)-([a-z_-]+)-(page)-([0-9]+)/?$ nation_notes.html?country=$1&title=$2&page=$3&pagenum=$4  [L,NC] # rewrite for country with page number

RewriteRule ^country/([0-9]+)-([a-z_-]+)/?$ nation_notes.html?country=$1&title=$2-page-1 [L,NC] # rewrite for country without page number

RewriteRule ^banknote/([0-9]+)-([a-z0-9_-]+)-p([a-zA-Z0-9_-]+)-([0-9]+)-([a-zA-Z_-]+)-from-([0-9]+)/?$ view_banknote.html?id=$1&countryname=$2&pick=$3&denomination=$4&currency=$5&year=$6 [L,NC] # rwrite for banknote

RewriteRule ^collector/([0-9]+)-([a-zA-Z0-9_.-]+)/?$ view_collector.html?user=$1&name=$2 [L,NC] # rwrite for collector

RewriteRule ^collection/([0-9]+)-([a-zA-Z0-9_.-]+)/?$ view_collection.html?collection=$1 [L,NC] # rwrite for collection

RewriteRule ^collection-country/([0-9]+)-([a-zA-Z0-9_.-]+)-([0-9]+)-([a-zA-Z_.-]+)/?$ view_collection.html?collection=$1&country=$3 [L,NC] # rwrite for country within collection

RewriteRule ^news/([0-9]+)-([a-zA-Z0-9_-]+)/?$ view_article.html?news=$1&title=$2 [L,NC] # rwrite for news articles

RewriteRule ^nationinfo/([0-9]+)-([a-zA-Z0-9_-]+)/?$ nation_info.html?country=$1&title=$2 [L,NC] # rwrite for country info

RewriteRule ^search-results-([0-9]+)/?$ banknote_search.html?resultpage=$1 [L,NC] # rwrite for banknote search results

RewriteRule ^search-results-([a-zA-z0-9]+)/?$ banknote_search.html [L,NC] # rwrite for banknote search results with non numeric page num

RewriteRule ^viewnote.html/?$ money_gallery.html [L,NC] 

RewriteRule ^nation.html/?$ money_gallery.html [L,NC] 

RewriteCond %{HTTP_HOST} !=""
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTPS}s ^on(s)|
RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

RewriteCond %{HTTP_REFERER} !^http://realbanknotes.com/.*$      [NC]
RewriteCond %{HTTP_REFERER} !^http://realbanknotes.com$      [NC]
RewriteCond %{HTTP_REFERER} !^http://www.realbanknotes.com/.*$      [NC]
RewriteCond %{HTTP_REFERER} !^http://www.realbanknotes.com$      [NC]
RewriteCond %{HTTP_REFERER} !^https://realbanknotes.com/.*$      [NC]
RewriteCond %{HTTP_REFERER} !^https://realbanknotes.com$      [NC]
RewriteCond %{HTTP_REFERER} !^https://www.realbanknotes.com/.*$      [NC]
RewriteCond %{HTTP_REFERER} !^https://www.realbanknotes.com$      [NC]
.htaccess mod-rewrite url-rewriting
1个回答
0
投票

您可以使用以下 htaccess 规则文件。我还在您的规则文件中做了一些修复。还要确保您的 .htaccess 文件存在于

public_html
文件夹中;在测试 URL 之前清除浏览器缓存。

RewriteEngine On

RewriteBase /home2/username/public_html/

RewriteCond %{HTTP_HOST} !=""
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTPS}s ^on(s)|
RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

# rewrite for country with page number
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
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 ^country/([0-9]+)-([a-z_-]+)-(page)-([0-9]+)/?$ nation_notes.html?country=$1&title=$2&page=$3&pagenum=$4  [L,NC]

# rewrite for country without page number
RewriteRule ^country/([0-9]+)-([a-z_-]+)/?$ nation_notes.html?country=$1&title=$2-page-1 [L,NC]

# rwrite for banknote
RewriteRule ^banknote/([0-9]+)-([a-z0-9_-]+)-p([a-zA-Z0-9_-]+)-([0-9]+)-([a-zA-Z_-]+)-from-([0-9]+)/?$ view_banknote.html?id=$1&countryname=$2&pick=$3&denomination=$4&currency=$5&year=$6 [L,NC]

# rwrite for collector
RewriteRule ^collector/([0-9]+)-([a-zA-Z0-9_.-]+)/?$ view_collector.html?user=$1&name=$2 [L,NC]

# rwrite for collection
RewriteRule ^collection/([0-9]+)-([a-zA-Z0-9_.-]+)/?$ view_collection.html?collection=$1 [L,NC]

# rwrite for country within collection
RewriteRule ^collection-country/([0-9]+)-([a-zA-Z0-9_.-]+)-([0-9]+)-([a-zA-Z_.-]+)/?$ view_collection.html?collection=$1&country=$3 [L,NC]

# rwrite for news articles
RewriteRule ^news/([0-9]+)-([a-zA-Z0-9_-]+)/?$ view_article.html?news=$1&title=$2 [L,NC]

# rwrite for country info
RewriteRule ^nationinfo/([0-9]+)-([a-zA-Z0-9_-]+)/?$ nation_info.html?country=$1&title=$2 [L,NC] 

# rwrite for banknote search results
RewriteRule ^search-results-([0-9]+)/?$ banknote_search.html?resultpage=$1 [L,NC] 

# rwrite for banknote search results with non numeric page num
RewriteRule ^search-results-([a-zA-z0-9]+)/?$ banknote_search.html [L,NC]

RewriteRule ^(viewnote|nation).html/?$ money_gallery.html [L,NC]


RewriteCond %{HTTP_REFERER} !^http://realbanknotes.com/.*$      [NC]
RewriteCond %{HTTP_REFERER} !^http://realbanknotes.com$      [NC]
RewriteCond %{HTTP_REFERER} !^http://www.realbanknotes.com/.*$      [NC]
RewriteCond %{HTTP_REFERER} !^http://www.realbanknotes.com$      [NC]
RewriteCond %{HTTP_REFERER} !^https://realbanknotes.com/.*$      [NC]
RewriteCond %{HTTP_REFERER} !^https://realbanknotes.com$      [NC]
RewriteCond %{HTTP_REFERER} !^https://www.realbanknotes.com/.*$      [NC]
RewriteCond %{HTTP_REFERER} !^https://www.realbanknotes.com$      [NC]
© www.soinside.com 2019 - 2024. All rights reserved.