【问题标题】:Redirecting to specific page on 404 errors if url contains a string in htaccess如果 url 在 htaccess 中包含字符串,则重定向到 404 错误的特定页面
【发布时间】:2017-04-20 17:56:12
【问题描述】:

如果用户尝试使用包含“/product/”的 url 访问页面并收到 404 错误,我会尝试重定向到特定页面(商店页面)。

这是我目前在我的 htaccess 中拥有的:

重写规则 ^/product/.* /store [R=404,L]

我需要进行哪些更改才能使其正常工作?

这是我当前的 htaccess 文件:

RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ www.mydomain.com/$1 [R=301,L]

# Send surplus store 404 errors to the store home page
ErrorDocument 404 /store
RewriteEngine On

RewriteRule ^/?product(/.*)?$ - [R=404,L,NC]

# handle both specific URL redirects for woocommerce
RewriteRule ^/?product-(?:tag|categories|category)/([\w-]+) www.mydomain.com/parts?category=$1 [R=301,L,NC,QSA]

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

# END WordPress

RewriteCond %{HTTP_HOST} ^mydomain\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.mydomain\.com$
RewriteRule ^MyAccount\.aspx\/?(.*)$ "http\:\/\/online\.mydomain\.com\/$1" [R=301,L]

【问题讨论】:

    标签: php .htaccess


    【解决方案1】:

    您可以在站点根目录 .htaccess 中使用此代码:

    # BEGIN WordPress
    RewriteEngine On
    RewriteBase /
    
    # handle both specific URL redirects for woocommerce
    RewriteRule ^/?product-(?:tag|categories|category)/([\w-]+) www.mydomain.com/parts?category=$1 [R=301,L,NC,QSA,NE]
    
    RewriteCond %{HTTP_HOST} ^(?:www\.)?mydomain\.com$ [NC]
    RewriteRule ^MyAccount\.aspx(/.*)?$ http://online.mydomain.com$1 [R=301,L,NC,NE]
    
    # redirect /product URIs
    RewriteRule ^/?product(/.*)?$ /store [R=301,L,NC]
    
    RewriteRule ^index\.php$ - [L]
    
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule . /index.php [L]
    
    # END WordPress
    

    【讨论】:

    • 感谢您的回答,但我无法使用此网址 www.mydomain.com/product/t414751r-exchdpf-filter
    • 好的,我已经用我的 htaccess 更新了这个问题,我不得不删除一些 https://
    • 啊,这是 wordpress :( 您必须通过主题的 404 处理程序以不同的方式进行操作,并且可能是 WP 重写 API
    • 或者,如果您可以通过301/302 完全重定向到/store,那么我们可以通过RewriteRule 进行操作
    • 是的,不幸的是,这是 wordpress。我想我可以用 301/302 重定向它
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-10-05
    • 2014-02-26
    • 2023-03-13
    • 2011-03-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多