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

Posted

技术标签:

【中文标题】HTACCESS RewriteRules 带有漂亮的 SEO 网址(不允许访问物理文件位置)【英文标题】:HTACCESS RewriteRules with pretty urls for SEO (without allowing access to physical file location) 【发布时间】:2021-12-23 01:33:12 【问题描述】:

我一直在四处寻找,但似乎无法找到我的问题的确切答案。对于这个例子,我将使用 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-us url 时加载了 /pages/about.php 文件。这可以正常工作。

我的问题来自当有人试图通过转到 domain.com/pages/about.php 直接访问该 url 时,我不希望这种可能性或影响我的 SEO基本上说同一页面有2个URL。因此,我通过执行以下代码之类的操作来阅读这一点。但它实际上只是保留输入的 URL,而不是做我需要做的事情。

RewriteCond %THE_REQUEST \s/about\.php\?lang=en\s [NC]
RewriteRule ^ /en/about-us? [R=301,L]

RewriteRule ^en/about-us$ /pages/about.php?lang=en [L]

非常感谢任何帮助或澄清

编辑

我也在下面尝试过这个,但它显然只是将语言添加到查询字符串中的无限循环。

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]

【问题讨论】:

【参考方案1】:

第一个解决方案:根据您显示的尝试/示例,请尝试遵循 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=en\s [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=fr\s [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]

【讨论】:

像魅力一样工作,非常感谢。

以上是关于HTACCESS RewriteRules 带有漂亮的 SEO 网址(不允许访问物理文件位置)的主要内容,如果未能解决你的问题,请参考以下文章

在不使用 .htaccess 文件的情况下,我应该将 RewriteRules 放在 Apache2 的哪里?

RewriteRules 不加载某些 css。 js和图片

使用 RewriteRules 和 404 进行 301 重定向

在 Apache/PHP 中显示所有活动的 RewriteRules

htaccess 中的 [QSA, L] 中的 L 是啥

symfony2 重写规则 .htaccess app.php