HTACCESS RewriteRules具有漂亮的SEO URL(不允许访问物理文件位置)



我一直在四处寻找,似乎找不到问题的确切答案。在这个例子中,我将使用domain.com作为我的域名。

我有htaccess规则,可以将漂亮的url重定向到web服务器中的实际页面。就像这个

RewriteEngine on
RewriteBase /
...
RewriteCond %{QUERY_STRING} .+
RewriteRule ^(fr|en)/(a-propos|about-us)$ pages/about.php?lang=$1&%{QUERY_STRING}
RewriteRule ^(fr|en)/(a-propos|about-us)$ pages/about.php?lang=$1

正如您所看到的,这基本上是在访问domain.com/en/about-usurl时加载/pages/about.php文件。这是正常的。

我的问题来自于当有人试图通过访问domain.com/pages/about.php直接访问url时,我不希望这是一种可能性,也不希望因为基本上说同一页面有两个url而影响我的SEO。所以我读到,通过做一些类似下面代码的事情会起作用。但实际上它只是保留输入的URL,而不是做我需要它做的事情。

RewriteCond %{THE_REQUEST} s/about.php?lang=ens [NC]
RewriteRule ^ /en/about-us? [R=301,L]
RewriteRule ^en/about-us$ /pages/about.php?lang=en [L]

非常感谢任何帮助或澄清

编辑

我在下面也尝试过,但很明显,它只是将lang添加到查询字符串中进行了一个无休止的循环。

RewriteCond %{REQUEST_URI} ^/pages/about.php [NC]
RewriteCond %{QUERY_STRING} ^lang=en
RewriteRule ^ en/about-us [R=301,L]
RewriteCond %{REQUEST_URI} ^/pages/about.php [NC]
RewriteCond %{QUERY_STRING} ^lang=fr
RewriteRule ^ fr/a-propos [R=301,L]
RewriteRule ^(fr|en)/(a-propos|about-us)$ pages/about.php?lang=$1&%{QUERY_STRING} [L]
RewriteRule ^(fr|en)/(a-propos|about-us)$ pages/about.php?lang=$1 [L]

第一个解决方案: 使用您显示的尝试/示例,请尝试以下htaccess规则文件。请确保在测试URL之前清除浏览器缓存。

RewriteEngine ON
##Direct request to url for about.php where query string has language as en.
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{REQUEST_URI} ^/pages/about.php [NC]
RewriteCond %{QUERY_STRING} ^lang=en [NC]
RewriteRule ^ en/about-us? [R=301,L]
RewriteRule ^(en)/about-us/?$ pages/about.php?lang=$1 [NC,L]
##Direct request to url for about.php where query string has language as fr.
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{REQUEST_URI} ^/pages/about.php [NC]
RewriteCond %{QUERY_STRING} ^lang=fr
RewriteRule ^ fr/a-propos? [R=301,L]
RewriteRule ^(fr)/about-us/?$ pages/about.php?lang=$1 [NC,L]
##Existing rules by OP with additonal conditions added to them.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(fr|en)/(a-propos|about-us)/?$ pages/about.php?lang=$1&%{QUERY_STRING} [NC,L]
##Existing rules by OP with additonal conditions added to them.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(fr|en)/(a-propos|about-us)/?$ pages/about.php?lang=$1 [NC,QSA,L]


第二个解决方案:或者尝试使用THE_REQUEST变量遵循htaccess规则集。请确保一次只使用一个以上或以下规则。

RewriteEngine ON
##Direct request to url for about.php where query string has language as en.
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{THE_REQUEST} s/pages/about.php?lang=ens [NC]
RewriteRule ^ en/about-us? [R=301,L]
RewriteRule ^(en)/about-us/?$ pages/about.php?lang=$1 [NC,L]
##Direct request to url for about.php where query string has language as fr.
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{THE_REQUEST} s/pages/about.php?lang=frs [NC]
RewriteRule ^ fr/a-propos? [R=301,L]
RewriteRule ^(fr)/about-us/?$ pages/about.php?lang=$1 [NC,L]
##Existing rules by OP with addiotnal conditions added to them.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(fr|en)/(a-propos|about-us)$ pages/about.php?lang=$1&%{QUERY_STRING} [NC,L]
##Existing rules by OP with addiotnal conditions added to them.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(fr|en)/(a-propos|about-us)$ pages/about.php?lang=$1 [NC,QSA,L]

最新更新