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

HTACCESS RewriteRules with pretty urls for SEO (without allowing access to physical file location)

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

我有 htaccess 规则,可以将 url 重定向到 Web 服务器中的实际页面。就这样

RewriteEngine on
RewriteBase /

...

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

如您所见,这基本上是在访问 domain.com/en/about-us 时加载 /pages/about.php 文件 url。这可以正常工作。

我的问题来自于有人试图通过转到 domain.com/pages/about.php[=32= 直接访问 url ],我不希望这成为可能或影响我的 SEO,因为基本上说同一页面有 2 URLs。所以我通过做类似下面的代码的事情来读到它会起作用。但它实际上只是保留了输入的 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]

非常感谢任何帮助或澄清

编辑

我也在下面尝试过,但它显然只是在无限循环中将 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=&%{QUERY_STRING} [L]
RewriteRule ^(fr|en)/(a-propos|about-us)$ pages/about.php?lang= [L]

第一个解决方案: 根据您显示的 attempts/samples,请尝试遵循 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= [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= [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=&%{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= [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= [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= [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=&%{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= [NC,QSA,L]