htaccess代码,用于重定向所有内部链接

时间:2019-06-29 06:41:51

标签: apache .htaccess mod-rewrite

我以前的网站地址是: qaz.com/home-page /

我想将所有旧地址重定向到新域: wsx.com/home-page /

我可以针对主要地址执行此操作: qaz.com已使用以下代码正确重定向至wsx.com/home-page /:

RewriteRule ^/?$ "https\:\/\/wsx\.com\/home-page\/" [R=301,L]

,但内部链接例如: https://qaz.com/home-page/professional-theme/没有重定向到 https://wsx.com/home-page/professional-theme/

我的网站CMS是WordPress。 您能帮忙解决这个问题吗?

3 个答案:

答案 0 :(得分:0)

您可以使用Redirect指令代替mod_rewrite:

Rewrite / https://wsx.com/

如果两个服务器名均通过单个VHost配置提供:

<If "%{HTTP_HOST} != 'wsx.com'">
    Rewrite / https://wsx.com/
</If>

答案 1 :(得分:0)

在您的Wordpress文件夹中的.htaccess文件顶部选中此1:1(qaz.com/anything <> wsx.com/anything)重写规则

<LinearLayout
                android:orientation="horizontal"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="0.8"
                android:weightSum="3">

                <RelativeLayout
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_weight="1">
                 ....
                </RelativeLayout>

                <LinearLayout
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_weight="1"
                    android:orientation="vertical"
                    >

                    <ImageView
                        android:layout_width="84dp"
                        android:layout_height="84dp"
                        android:layout_centerInParent="true"
                        android:layout_above="@+id/meal_ln"
                        android:background="@drawable/meal_img_bg_ac"
                        app:layout_aspectRatio="100%"
                        />

                    <TextView
                        android:id="@+id/meal_ln"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:text="Lunch"
                        android:textAlignment="center"
                        android:textColor="@color/priTxtLight"
                        android:paddingTop="4dp"
                        android:paddingBottom="4dp"
                        android:fontFamily="casual"/>

                </LinearLayout>

                <LinearLayout
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_weight="1">
                 ....
                </LinearLayout>
        </LinearLayout>

或从qaz.com到wsx.com/home-page/的所有链接(qaz.com/anything <> wsx.com/home-page/)

RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^(www\.)?qaz\.com [NC]
RewriteRule (.*) https://wsx.com/$1 [R=301,L]

答案 2 :(得分:0)

这将是对您的确切问题的最接近答案:

RewriteEngine on
RewriteCond %{HOST_NAME} ^qaz\.com [NC]
RewriteRule ^/?home-page/(.+)$ https://wsx.com/home-page/$1 [R=301,QSA]

一个好主意是从302临时重定向开始,然后在确定一切正确设置之后,才将其更改为301永久重定向。这样可以防止在尝试时缓存问题...

此规则将同样在HTTP服务器主机配置或动态配置文件内(“htaccess的”文件)工作。显然,重写模块需要加载到http服务器内部并在http主机中启用。如果使用动态配置文件,则需要注意在主机配置中完全启用了它的解释,并且该解释位于主机的DOCUMENT_ROOT文件夹中。

还有一个一般性说明:您应该始终喜欢将此类规则放置在http服务器主机配置中,而不要使用动态配置文件(“ .htaccess”)。这些动态配置文件增加了复杂性,通常是导致意外行为,难以调试的原因,并且确实降低了http服务器的速度。仅当您无法访问真正的http服务器主机配置(阅读:真正便宜的服务提供商)或坚持编写自己的规则的应用程序(这显然是安全的噩梦)时,才提供它们作为最后的选择。