htaccess的规则,重定向老所没有的地址,新地址地址、有的、重定向、规则

2023-09-02 00:53:02 作者:浪荡

下面是如何寻找在博客上我的老帖子不会忽略:

  subdomain.domain.com/view/1310/article-title/
 

我想它当访问者来自谷歌这样一个地址,重定向,像这样:

  http://www.domain.com/article-title/
 
laravel修改.htaccess文件重定向public的解决方法 其它代码类资源 CSDN下载

我需要指定有关旧/第一个环节的一些细节:

子域名,是一个变量 查看,是一个固定的字 1230 ,C和任意数量/ ID

我尝试这样做:

 选项+了FollowSymLinks
RewriteEngine叙述上
的RewriteCond%{HTTP_HOST} subdomain.domain.com $ [NC]
重写规则^ /视图/(*)/(.*)$ http://www.domain.com/$2 [R = 301,L]
 

产生一个大的500错误。

网站被裁定就一个字preSS厘米。

在此先感谢!

补充后,迈克尔Berkowski的回答。我现在的WP规则是:

 < IfModule mod_rewrite.c>
RewriteEngine叙述上
的RewriteBase /
重写规则^指数 .PHP $  -  [L]
的RewriteCond%{} REQUEST_FILENAME!-f
的RewriteCond%{} REQUEST_FILENAME!-d
重写规则。的index.php [L]
< / IfModule>
 

解决方案

500错误是结果的(*)其中 * (零个或多个)无关preceding它作为一个限定词。你可能打算(。*),但你真正需要的有 [^ /] + 来获取所有字符,直到下一个 /

 选项+了FollowSymLinks
RewriteEngine叙述上
#子域的稍作修改 - 必须逃脱。
的RewriteCond%{HTTP_HOST}子域。域 .COM $ [NC]
#请注意,这可能需要^视图/,而不是^ /视图
#在htaccess的情况下领先/也许应该不会有
重写规则^ /视图/([^/]+)/(.*)$ http://www.domain.com/$2 [R = 301,L]
 

以上看起来专为 subdomain.domain.com ,但既然你指定它是可变的,以此来获得,除了 WWW所有子域。 domain.com

 选项+了FollowSymLinks
RewriteEngine叙述上
#匹配所有子域名,除了WWW。
的RewriteCond%{HTTP_HOST}!^ WWW 。域 .COM $ [NC]
重写规则^ /视图/([^/]+)/(.*)$ http://www.domain.com/$2 [R = 301,L]
 

如果做不到这一点,上传你可能有(因为你提到这个词preSS我希望你有其他规定),因为该命令可能是显著任何其他重写规则。

更新将字preSS规则:

 < IfModule mod_rewrite.c>
RewriteEngine叙述上
的RewriteBase /

#新规则处理子域名重定向...
的RewriteCond%{HTTP_HOST}!^ WWW 。域 .COM $ [NC]
重写规则^视图/([^/]+)/(.*)$ http://www.domain.com/$2 [R = 301,L]

#这字preSS做它的内部重定向后
重写规则^指数 .PHP $  -  [L]
的RewriteCond%{} REQUEST_FILENAME!-f
的RewriteCond%{} REQUEST_FILENAME!-d
重写规则。的index.php [L]
< / IfModule>
 

Here is how are looking my old post adresses on a blog:

subdomain.domain.com/view/1310/article-title/

I'd like it when a visitor comes from Google on such an address, to be redirected like so:

http://www.domain.com/article-title/

I need to specify some details regarding the old/first link:

subdomain, is a variable view, is a fixed word 1230, cand be any number/id

I tried this:

Options +FollowSymLinks
RewriteEngine On
RewriteCond %{HTTP_HOST} subdomain.domain.com $ [NC]
RewriteRule ^/view/(*)/(.*)$ http://www.domain.com/$2 [R=301,L]

Resulted on a big 500 error.

Site is ruling on a WordPress cms.

Thanks in advance!

Added after Michael Berkowski's answer. My current wp rules are:

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

解决方案

The 500 error is a result of the (*) where the * (zero or more) has nothing preceding it as a qualifier. You might have intended (.*) but what you really need in there is [^/]+ to get all characters up to the next /:

Options +FollowSymLinks
RewriteEngine On
# Slight modification of the subdomain - must escape .
RewriteCond %{HTTP_HOST} subdomain.domain.com$ [NC]
# Note this may need to be ^view/ instead of ^/view
# In htaccess context the leading / should probably not be there
RewriteRule ^/view/([^/]+)/(.*)$ http://www.domain.com/$2 [R=301,L]

The above looks specifically for subdomain.domain.com, but since you specified it is variable, use this to get all subdomains except www.domain.com:

Options +FollowSymLinks
RewriteEngine On
# Matches all subdomains except www.
RewriteCond %{HTTP_HOST} !^www.domain.com$ [NC]
RewriteRule ^/view/([^/]+)/(.*)$ http://www.domain.com/$2 [R=301,L]

Failing this, post any other rewrite rules you may have (since you mentioned this is WordPress I expect you have other rules) because the order is likely to be significant.

Update incorporate WordPress rules:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /

# The new rules handle the subdomain redirect...
RewriteCond %{HTTP_HOST} !^www.domain.com$ [NC]
RewriteRule ^view/([^/]+)/(.*)$ http://www.domain.com/$2 [R=301,L]

# After which WordPress does its internal redirection
RewriteRule ^index.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>