我希望https://www.example.com/ca-report/被重定向到https://www.example.com/的头版。
但是,当我试图在https://www.example.com/ca-report/的.htaccess中放置301重定向时,不幸的是,子页https://www.example.com/ca-report/subpage已经无法访问了。
我想我做错了什么。你能帮我检查一下我的.htaccess代码吗?
顺便说一句:我不确定是否允许我像以前一样多次使用RewriteEngine On。
以下是我失败的尝试:
# BEGIN iThemes Security - Do not modify or remove this line
# iThemes Security Config Details: 2
# Protect System Files - Security > Settings > System Tweaks > System Files
<files .htaccess>
<IfModule mod_authz_core.c>
Require all denied
</IfModule>
<IfModule !mod_authz_core.c>
Order allow,deny
Deny from all
</IfModule>
</files>
<files readme.html>
<IfModule mod_authz_core.c>
Require all denied
</IfModule>
<IfModule !mod_authz_core.c>
Order allow,deny
Deny from all
</IfModule>
</files>
<files readme.txt>
<IfModule mod_authz_core.c>
Require all denied
</IfModule>
<IfModule !mod_authz_core.c>
Order allow,deny
Deny from all
</IfModule>
</files>
<files wp-config.php>
<IfModule mod_authz_core.c>
Require all denied
</IfModule>
<IfModule !mod_authz_core.c>
Order allow,deny
Deny from all
</IfModule>
</files>
# Disable Directory Browsing - Security > Settings > System Tweaks > Directory Browsing
Options -Indexes
<IfModule mod_rewrite.c>
RewriteEngine On
# Protect System Files - Security > Settings > System Tweaks > System Files
RewriteRule ^wp-admin/install\.php$ - [F]
RewriteRule ^wp-admin/includes/ - [F]
RewriteRule !^wp-includes/ - [S=3]
RewriteRule ^wp-includes/[^/]+\.php$ - [F]
RewriteRule ^wp-includes/js/tinymce/langs/.+\.php - [F]
RewriteRule ^wp-includes/theme-compat/ - [F]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule (^|.*/)\.(git|svn)/.* - [F]
# Disable PHP in Uploads - Security > Settings > System Tweaks > PHP in Uploads
RewriteRule ^wp\-content/uploads/.*\.(?:php[1-7]?|pht|phtml?|phps)\.?$ - [NC,F]
# Disable PHP in Plugins - Security > Settings > System Tweaks > PHP in Plugins
RewriteRule ^wp\-content/plugins/.*\.(?:php[1-7]?|pht|phtml?|phps)\.?$ - [NC,F]
# Disable PHP in Themes - Security > Settings > System Tweaks > PHP in Themes
RewriteRule ^wp\-content/themes/.*\.(?:php[1-7]?|pht|phtml?|phps)\.?$ - [NC,F]
</IfModule>
# END iThemes Security - Do not modify or remove this line
# BEGIN WordPress
# The directives (lines) between "BEGIN WordPress" and "END WordPress" are
# dynamically generated, and should only be modified via WordPress filters.
# Any changes to the directives between these markers will be overwritten.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
# redirect to https & www
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www.example.com$ [NC]
RewriteRule ^(.*)$ https://www.example.com/$1 [L,R=301]
RewriteCond %{SERVER_PORT} !=443
RewriteRule ^(.*)$ https://www.example.com/$1 [R=301,L]
</IfModule>
# 301-redirect
RewriteEngine On
Redirect 301 /ca-report/ https://www.example.com/
<ifModule mod_gzip.c>
mod_gzip_on Yes
mod_gzip_dechunk Yes
mod_gzip_item_include file .(html?|txt|css|js|php|pl)$
mod_gzip_item_include handler ^cgi-script$
mod_gzip_item_include mime ^text/.*
mod_gzip_item_include mime ^application/x-javascript.*
mod_gzip_item_exclude mime ^image/.*
mod_gzip_item_exclude rspheader ^Content-Encoding:.*gzip.*
</ifModule>
<IfModule mod_deflate.c>
AddOutputFilterByType DEFLATE text/plain
AddOutputFilterByType DEFLATE text/html
AddOutputFilterByType DEFLATE text/xml
AddOutputFilterByType DEFLATE text/css
AddOutputFilterByType DEFLATE application/xml
AddOutputFilterByType DEFLATE application/xhtml+xml
AddOutputFilterByType DEFLATE application/rss+xml
AddOutputFilterByType DEFLATE application/javascript
AddOutputFilterByType DEFLATE application/x-javascript
</IfModule>编辑:
@怀特先生,经过你的详细解释,我对htaccess有了更多的了解。正如我所说,由于另一个插件,htaccess代码在此期间略有变化。此外,我还为www.example.com/ca-writing/添加了一个重定向。
www.example.com
www.example.com/ca-report/ #should be redirected to front page www.example.com
www.example.com/ca-report/subpage # should be accessible normally
www.example.com/ca-writing/ # should be redirected to front page www.example.com for SEO-reason, since this URL doesn’t exist anymore after the website restructuring我已经实现了您的提示,这里是我最后的htaccess代码:
# BEGIN iThemes Security - Do not modify or remove this line
# iThemes Security Config Details: 2
# Protect System Files - Security > Settings > System Tweaks > System Files
<files .htaccess>
<IfModule mod_authz_core.c>
Require all denied
</IfModule>
<IfModule !mod_authz_core.c>
Order allow,deny
Deny from all
</IfModule>
</files>
<files readme.html>
<IfModule mod_authz_core.c>
Require all denied
</IfModule>
<IfModule !mod_authz_core.c>
Order allow,deny
Deny from all
</IfModule>
</files>
<files readme.txt>
<IfModule mod_authz_core.c>
Require all denied
</IfModule>
<IfModule !mod_authz_core.c>
Order allow,deny
Deny from all
</IfModule>
</files>
<files wp-config.php>
<IfModule mod_authz_core.c>
Require all denied
</IfModule>
<IfModule !mod_authz_core.c>
Order allow,deny
Deny from all
</IfModule>
</files>
# Disable Directory Browsing - Security > Settings > System Tweaks > Directory Browsing
Options -Indexes
<IfModule mod_rewrite.c>
RewriteEngine On
# Protect System Files - Security > Settings > System Tweaks > System Files
RewriteRule ^wp-admin/install\.php$ - [F]
RewriteRule ^wp-admin/includes/ - [F]
RewriteRule !^wp-includes/ - [S=3]
RewriteRule ^wp-includes/[^/]+\.php$ - [F]
RewriteRule ^wp-includes/js/tinymce/langs/.+\.php - [F]
RewriteRule ^wp-includes/theme-compat/ - [F]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule (^|.*/)\.(git|svn)/.* - [F]
# Disable PHP in Uploads - Security > Settings > System Tweaks > PHP in Uploads
RewriteRule ^wp\-content/uploads/.*\.(?:php[1-7]?|pht|phtml?|phps)\.?$ - [NC,F]
# Disable PHP in Plugins - Security > Settings > System Tweaks > PHP in Plugins
RewriteRule ^wp\-content/plugins/.*\.(?:php[1-7]?|pht|phtml?|phps)\.?$ - [NC,F]
# Disable PHP in Themes - Security > Settings > System Tweaks > PHP in Themes
RewriteRule ^wp\-content/themes/.*\.(?:php[1-7]?|pht|phtml?|phps)\.?$ - [NC,F]
</IfModule>
# END iThemes Security - Do not modify or remove this line
# php memory limit increase (only temporary)
php_value memory_limit 800M
# Redirect "/ca-report/" & "/ca-writing/" to root (and HTTPS / www)
RewriteRule ^ca-report/$ https://www.example.com/ [R=301,L]
RewriteRule ^ ca-writing /$ https://www.example.com/ [R=301,L]
# Redirect to HTTPS & www
RewriteCond %{HTTP_HOST} !^www\.example\.com$ [NC]
RewriteRule (.*) https://www.example.com/$1 [L,R=301]
RewriteCond %{SERVER_PORT} !=443
RewriteRule (.*) https://www.example.com/$1 [R=301,L]
<ifModule mod_gzip.c>
mod_gzip_on Yes
mod_gzip_dechunk Yes
mod_gzip_item_include file .(html?|txt|css|js|php|pl)$
mod_gzip_item_include handler ^cgi-script$
mod_gzip_item_include mime ^text/.*
mod_gzip_item_include mime ^application/x-javascript.*
mod_gzip_item_exclude mime ^image/.*
mod_gzip_item_exclude rspheader ^Content-Encoding:.*gzip.*
</ifModule>
<IfModule mod_deflate.c>
AddOutputFilterByType DEFLATE text/plain
AddOutputFilterByType DEFLATE text/html
AddOutputFilterByType DEFLATE text/xml
AddOutputFilterByType DEFLATE text/css
AddOutputFilterByType DEFLATE application/xml
AddOutputFilterByType DEFLATE application/xhtml+xml
AddOutputFilterByType DEFLATE application/rss+xml
AddOutputFilterByType DEFLATE application/javascript
AddOutputFilterByType DEFLATE application/x-javascript
</IfModule>
# BEGIN W3TC Browser Cache
<IfModule mod_mime.c>
AddType text/css .css
AddType text/x-component .htc
AddType application/x-javascript .js
AddType application/javascript .js2
AddType text/javascript .js3
AddType text/x-js .js4
AddType video/asf .asf .asx .wax .wmv .wmx
AddType video/avi .avi
AddType image/avif .avif
AddType image/avif-sequence .avifs
AddType image/bmp .bmp
AddType application/java .class
AddType video/divx .divx
AddType application/msword .doc .docx
AddType application/vnd.ms-fontobject .eot
AddType application/x-msdownload .exe
AddType image/gif .gif
AddType application/x-gzip .gz .gzip
AddType image/x-icon .ico
AddType image/jpeg .jpg .jpeg .jpe
AddType image/webp .webp
AddType application/json .json
AddType application/vnd.ms-access .mdb
AddType audio/midi .mid .midi
AddType video/quicktime .mov .qt
AddType audio/mpeg .mp3 .m4a
AddType video/mp4 .mp4 .m4v
AddType video/mpeg .mpeg .mpg .mpe
AddType video/webm .webm
AddType application/vnd.ms-project .mpp
AddType application/x-font-otf .otf
AddType application/vnd.ms-opentype ._otf
AddType application/vnd.oasis.opendocument.database .odb
AddType application/vnd.oasis.opendocument.chart .odc
AddType application/vnd.oasis.opendocument.formula .odf
AddType application/vnd.oasis.opendocument.graphics .odg
AddType application/vnd.oasis.opendocument.presentation .odp
AddType application/vnd.oasis.opendocument.spreadsheet .ods
AddType application/vnd.oasis.opendocument.text .odt
AddType audio/ogg .ogg
AddType video/ogg .ogv
AddType application/pdf .pdf
AddType image/png .png
AddType application/vnd.ms-powerpoint .pot .pps .ppt .pptx
AddType audio/x-realaudio .ra .ram
AddType image/svg+xml .svg .svgz
AddType application/x-shockwave-flash .swf
AddType application/x-tar .tar
AddType image/tiff .tif .tiff
AddType application/x-font-ttf .ttf .ttc
AddType application/vnd.ms-opentype ._ttf
AddType audio/wav .wav
AddType audio/wma .wma
AddType application/vnd.ms-write .wri
AddType application/font-woff .woff
AddType application/font-woff2 .woff2
AddType application/vnd.ms-excel .xla .xls .xlsx .xlt .xlw
AddType application/zip .zip
</IfModule>
<IfModule mod_expires.c>
ExpiresActive On
ExpiresByType text/css A31536000
ExpiresByType text/x-component A31536000
ExpiresByType application/x-javascript A31536000
ExpiresByType application/javascript A31536000
ExpiresByType text/javascript A31536000
ExpiresByType text/x-js A31536000
ExpiresByType video/asf A31536000
ExpiresByType video/avi A31536000
ExpiresByType image/avif A31536000
ExpiresByType image/avif-sequence A31536000
ExpiresByType image/bmp A31536000
ExpiresByType application/java A31536000
ExpiresByType video/divx A31536000
ExpiresByType application/msword A31536000
ExpiresByType application/vnd.ms-fontobject A31536000
ExpiresByType application/x-msdownload A31536000
ExpiresByType image/gif A31536000
ExpiresByType application/x-gzip A31536000
ExpiresByType image/x-icon A31536000
ExpiresByType image/jpeg A31536000
ExpiresByType image/webp A31536000
ExpiresByType application/json A31536000
ExpiresByType application/vnd.ms-access A31536000
ExpiresByType audio/midi A31536000
ExpiresByType video/quicktime A31536000
ExpiresByType audio/mpeg A31536000
ExpiresByType video/mp4 A31536000
ExpiresByType video/mpeg A31536000
ExpiresByType video/webm A31536000
ExpiresByType application/vnd.ms-project A31536000
ExpiresByType application/x-font-otf A31536000
ExpiresByType application/vnd.ms-opentype A31536000
ExpiresByType application/vnd.oasis.opendocument.database A31536000
ExpiresByType application/vnd.oasis.opendocument.chart A31536000
ExpiresByType application/vnd.oasis.opendocument.formula A31536000
ExpiresByType application/vnd.oasis.opendocument.graphics A31536000
ExpiresByType application/vnd.oasis.opendocument.presentation A31536000
ExpiresByType application/vnd.oasis.opendocument.spreadsheet A31536000
ExpiresByType application/vnd.oasis.opendocument.text A31536000
ExpiresByType audio/ogg A31536000
ExpiresByType video/ogg A31536000
ExpiresByType application/pdf A31536000
ExpiresByType image/png A31536000
ExpiresByType application/vnd.ms-powerpoint A31536000
ExpiresByType audio/x-realaudio A31536000
ExpiresByType image/svg+xml A31536000
ExpiresByType application/x-shockwave-flash A31536000
ExpiresByType application/x-tar A31536000
ExpiresByType image/tiff A31536000
ExpiresByType application/x-font-ttf A31536000
ExpiresByType application/vnd.ms-opentype A31536000
ExpiresByType audio/wav A31536000
ExpiresByType audio/wma A31536000
ExpiresByType application/vnd.ms-write A31536000
ExpiresByType application/font-woff A31536000
ExpiresByType application/font-woff2 A31536000
ExpiresByType application/vnd.ms-excel A31536000
ExpiresByType application/zip A31536000
</IfModule>
<IfModule mod_deflate.c>
AddOutputFilterByType DEFLATE text/css text/x-component application/x-javascript application/javascript text/javascript text/x-js text/html text/richtext text/plain text/xsd text/xsl text/xml image/bmp application/java application/msword application/vnd.ms-fontobject application/x-msdownload image/x-icon application/json application/vnd.ms-access video/webm application/vnd.ms-project application/x-font-otf application/vnd.ms-opentype application/vnd.oasis.opendocument.database application/vnd.oasis.opendocument.chart application/vnd.oasis.opendocument.formula application/vnd.oasis.opendocument.graphics application/vnd.oasis.opendocument.presentation application/vnd.oasis.opendocument.spreadsheet application/vnd.oasis.opendocument.text audio/ogg application/pdf application/vnd.ms-powerpoint image/svg+xml application/x-shockwave-flash image/tiff application/x-font-ttf application/vnd.ms-opentype audio/wav application/vnd.ms-write application/font-woff application/font-woff2 application/vnd.ms-excel
<IfModule mod_mime.c>
# DEFLATE by extension
AddOutputFilter DEFLATE js css htm html xml
</IfModule>
</IfModule>
<FilesMatch "\.(css|htc|less|js|js2|js3|js4|CSS|HTC|LESS|JS|JS2|JS3|JS4)$">
FileETag MTime Size
<IfModule mod_headers.c>
Header unset Set-Cookie
</IfModule>
</FilesMatch>
<FilesMatch "\.(html|htm|rtf|rtx|txt|xsd|xsl|xml|HTML|HTM|RTF|RTX|TXT|XSD|XSL|XML)$">
FileETag MTime Size
</FilesMatch>
<FilesMatch "\.(asf|asx|wax|wmv|wmx|avi|avif|avifs|bmp|class|divx|doc|docx|eot|exe|gif|gz|gzip|ico|jpg|jpeg|jpe|webp|json|mdb|mid|midi|mov|qt|mp3|m4a|mp4|m4v|mpeg|mpg|mpe|webm|mpp|otf|_otf|odb|odc|odf|odg|odp|ods|odt|ogg|ogv|pdf|png|pot|pps|ppt|pptx|ra|ram|svg|svgz|swf|tar|tif|tiff|ttf|ttc|_ttf|wav|wma|wri|woff|woff2|xla|xls|xlsx|xlt|xlw|zip|ASF|ASX|WAX|WMV|WMX|AVI|AVIF|AVIFS|BMP|CLASS|DIVX|DOC|DOCX|EOT|EXE|GIF|GZ|GZIP|ICO|JPG|JPEG|JPE|WEBP|JSON|MDB|MID|MIDI|MOV|QT|MP3|M4A|MP4|M4V|MPEG|MPG|MPE|WEBM|MPP|OTF|_OTF|ODB|ODC|ODF|ODG|ODP|ODS|ODT|OGG|OGV|PDF|PNG|POT|PPS|PPT|PPTX|RA|RAM|SVG|SVGZ|SWF|TAR|TIF|TIFF|TTF|TTC|_TTF|WAV|WMA|WRI|WOFF|WOFF2|XLA|XLS|XLSX|XLT|XLW|ZIP)$">
FileETag MTime Size
<IfModule mod_headers.c>
Header unset Set-Cookie
</IfModule>
</FilesMatch>
<IfModule mod_headers.c>
Header set Referrer-Policy "no-referrer-when-downgrade"
</IfModule>
# END W3TC Browser Cache
# BEGIN WordPress
# The directives (lines) between "BEGIN WordPress" and "END WordPress" are
# dynamically generated, and should only be modified via WordPress filters.
# Any changes to the directives between these markers will be overwritten.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress我最后的问题是:
在重组之前,
https://www.example.com/ca-writing/,但它不再存在。我想重定向它的搜索引擎优化理由到头版。我是否仍然需要在Wordpress后端中创建这个子页面https://www.example.com/ca-report/,作为一个空页才能重定向它,尽管内容在https://www.example.com/ca-report/之后不再存在--请简要查看我完成的htaccess代码,并让我知道它看起来是否不错?。
感谢你的支持!
发布于 2022-09-19 01:27:45
BEGIN WordPress:# END WordPress #重定向到https & www &L;IfModule mod_rewrite.c> RewriteEngine On RewriteCond %{HTTP_HOST} !^www.example.com$ NC RewriteRule ^(.*)$ https://www.example.com/$1 L,R=301 RewriteCond %{SERVER_PORT} !=443 RewriteRule ^(.*)$ https://www.example.com/$1 R=301,L# 301 -在重定向301/ca上重定向RewriteEngine /# On 20#
mod_alias Redirect指令是前缀匹配,匹配后的所有内容都复制到目标URL的末尾。因此,上面的Redirect指令将“不正确”地将/ca-report/subpage重定向到https://www.example.com/subpage。(顺便说一句,Redirect与启动mod_rewrite的RewriteEngine无关。)
要匹配特定的URL,您的选项是要么使用相应的mod_alias RedirectMatch指令(它与正则表达式匹配,而不是简单的前缀匹配),要么使用mod_rewrite RewriteRule。但是,不宜将来自两个模块的重定向混合在一起,因为您可能会遇到意外的冲突,因为这两个模块在请求期间的不同时间独立运行。因此,由于您已经在整个配置文件中使用mod_rewrite (RewriteRule),所以您应该在这里使用RewriteRule指令来执行重定向。
然而,你的规则也是错误的。通过将此重定向更改为使用RewriteRule,它将永远不会在配置文件中(在WordPress代码块之后)被处理。这需要在WordPress代码块之前进行,即。在# BEGIN WordPress注释标记之前。这同样适用于您的HTTPS和www规范重定向--它们目前并不像您所期望的那样工作,因为它们只会重定向静态资源,而不是您的内部页面URL。
你的规则应该是这样写的:
# Redirect "/ca-report/" to root (and HTTPS / www)
RewriteRule ^ca-report/$ https://www.example.com/ [R=301,L]
# Redirect to HTTPS & www
RewriteCond %{HTTP_HOST} !^www\.example\.com$ [NC]
RewriteRule (.*) https://www.example.com/$1 [L,R=301]
RewriteCond %{SERVER_PORT} !=443
RewriteRule (.*) https://www.example.com/$1 [R=301,L]
# BEGIN WordPress
:
# END WordPress不需要<IfModule>包装器,也不需要重复RewriteEngine On指令。
--我不确定是否允许我像以前那样多次使用
RewriteEngine On。
但是,如果您正在手动编写配置文件的代码,则不应该重复RewriteEngine指令。
如果整个文件中有多个RewriteEngine指令,那么实际上是RewriteEngine指令的最后一个实例“获胜”并控制整个文件。前面的RewriteEngine指令根本不做任何事情,因此在这方面它可能会让人感到困惑!例如,如果您在文件的末尾编写了RewriteEngine Off,那么所有的mod_rewrite指令都会被禁用,尽管文件前面有(多个) RewriteEngine On指令。
注意:正如WP代码注释中所指出的,不要删除WordPress代码块中的WordPress指令,因为WordPress本身(试图)维护本节。
更新:
1A.我还需要在Wordpress后端中创建这个子页面
https://www.example.com/ca-report/作为一个空页面才能重定向它吗?
不,该页不需要存在。在.htaccess中重定向发生在将请求传递给WordPress/PHP之前。
1B.我想重定向它的搜索引擎优化理由到头版。
因为"SEO的原因“是值得怀疑的。搜索引擎/谷歌可能会看到一个重定向(从旧的/内页)到首页作为一个软404,所以可能不会提供任何真正的SEO好处。对于最终看到他们不希望看到的页面的用户来说,这也可能是令人困惑的。旧页面也可能在搜索结果中停留更长时间。在这些情况下,通常最好是发出一个自定义404响应,向用户解释页面的去向,并可能建议其他页面。这将吸引用户并将其保留在站点上,并且仍然通知搜索引擎页面不再存在。
将“/ca-report/&”& "/ ca-writing /“重定向到根(和HTTPS / www) RewriteRule ^ca-report/$ https://www.example.com/ R=301,L RewriteRule ^ca写入/$ https://www.example.com/ R=301,L
在第二个规则中,您似乎在RewriteRule模式中引入了一些错误的间距。^ ca-writing /$应该读取^ca-writing/$,否则会导致错误。(空格是Apache配置文件中的参数分隔符。)
但是,如果您愿意,可以使用regex交替组合这两个规则。例如:
RewriteRule ^ca-(report|writing)/$ https://www.example.com/ [R=301,L]https://stackoverflow.com/questions/73747339
复制相似问题