去除?从htaccess重定向并重写后从URL发出

发布于 2025-02-02 03:16:18 字数 948 浏览 3 评论 0原文

我已经建立了一个新网站,Google索引了一个URL,此后我想在新的URL中添加301个URL。 URL是SEF URL,因此已重写。

我设法在浏览器中使用正确的重写URL创建了从旧页面到新页面的301,但我的操作方式我有一个尾随?在我的网址结束时。

首先,这甚至重要吗? Google是否会在有和没有的情况下将其排名为同一页面?还是会用?作为重复内容的单独页面?

如果将其视为单独的页面,那么我该如何删除?在最后?

这是我在htaccess中的规则:

RewriteEngine on
Redirect 301 /search/toys/heroes-of-goo-jit-zu https://example.com/search/figures/heroes-of-goo-jit-zu?
RewriteBase /
RewriteCond %{HTTP_HOST} !^domain.com$ [NC]  
RewriteRule .? https://example.com%{REQUEST_URI} [R=301]
RewriteRule ^search/([a-zA-Z0-9.\-_]+)/([a-zA-Z0-9.\-_]+)/([a-zA-Z0-9.\-_]+) search.php?category=$1&subcategory=$2&subsubcategory=$3 [QSA]
RewriteRule ^search/([a-zA-Z0-9.\-_]+)/([a-zA-Z0-9.\-_]+) search.php?category=$1&subcategory=$2 [QSA]
RewriteRule ^search/([a-zA-Z0-9.\-_]+) search.php?category=$1 [QSA]

没有?在规则的末尾,我在我不想要的URL末尾获得了所有查询字符串。最后,我尝试了各种标志,[QSA]和[QSD]都会导致500个错误,这是我可以找到重定向并重写而不会导致500错误的唯一方法

I've made a new site and Googles indexed one of the URLs which Ive since changed so wanted to add a 301 to the new URL. The URLs are SEF URLs so have been rewritten.

Ive managed to create a 301 from the old page to the new with the correct rewritten URL in the browser but the way Ive done it I have a trailing ? at the end of my URL.

Firstly, does this even matter? Will Google rank it as the same page with and without the ? or will it treat the URL with the ? as a separate page of duplicate content?

If its treating it as a separate page then how can I remove this ? at the end?

Here's my rule in htaccess:

RewriteEngine on
Redirect 301 /search/toys/heroes-of-goo-jit-zu https://example.com/search/figures/heroes-of-goo-jit-zu?
RewriteBase /
RewriteCond %{HTTP_HOST} !^domain.com$ [NC]  
RewriteRule .? https://example.com%{REQUEST_URI} [R=301]
RewriteRule ^search/([a-zA-Z0-9.\-_]+)/([a-zA-Z0-9.\-_]+)/([a-zA-Z0-9.\-_]+) search.php?category=$1&subcategory=$2&subsubcategory=$3 [QSA]
RewriteRule ^search/([a-zA-Z0-9.\-_]+)/([a-zA-Z0-9.\-_]+) search.php?category=$1&subcategory=$2 [QSA]
RewriteRule ^search/([a-zA-Z0-9.\-_]+) search.php?category=$1 [QSA]

Without the ? on the end of the rule, I get all the query string on the end of the URL which I don't want. I have tried various flags on the end, both [QSA] and [QSD] cause 500 errors and this is the only method I could find to do the redirect and rewrite without causing 500 errors

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

温柔戏命师 2025-02-09 03:16:18

MOD_ALIAS 重定向指令将在目标URL上保留尾随。您应该使用mod_rewrite 重写指令,而不是根据需要进行操作并删除查询字符串。而且,由于您将mod_rewrite用于其他重定向/重写,因此应避免将两个模块重定向混合以避免意外冲突。 (尽管配置文件中有明显的指令顺序,但始终先处理mod_rewrite。)

使用mod_rewrite:

RewriteRule ^search/toys/heroes-of-goo-jit-zu$ https://example.com/search/figures/heroes-of-goo-jit-zu [QSD,R=301,L]

QSD(查询字符串丢弃)flag从重定向响应中删除原始查询字符串。

我在重写 模式上包括了串联锚($)到您的原始重定向指令(前缀匹配)。

您需要在测试之前清除浏览器高速缓存,因为较早的301被缓存。 (首先使用302-临时 - 重定向测试,以避免潜在的缓存问题。)

您还缺少所有其他规则上的l标志 - 应该添加这一点。至少,这可以防止对以后的规则进行不必要的处理,但是在规范重定向上l标志的情况下,这可能会导致错误的重定向,从而公开重写的URL。

更新:在有关QSA标志的评论中阐明讨论。这与您的直接问题无关。如果您希望在请求的URL上使用查询字符串,则需要QSA标志(为了 Merge ,查询字符串带有中所述的查询字符串替换字符串),否则,应删除此标志。

我在末尾尝试了各种标志,[QSA]和[QSD]导致500个错误

flags 参数仅适用于mod_rewrite(rewriterule and code> rewriteCond < /code>指令) - 这在mod_alias redirect上无效( redirectMatch )指令(s)。

The mod_alias Redirect directive will preserve the trailing ? on the target URL. You should use a mod_rewrite RewriteRule directive instead to do as you require and remove the query string. And since you are using mod_rewrite for other redirects/rewrites you should avoid mixing redirects from both modules in order to avoid accidental conflicts. (mod_rewrite is always processed first, despite the apparent order of directives in the config file.)

Using mod_rewrite instead:

RewriteRule ^search/toys/heroes-of-goo-jit-zu$ https://example.com/search/figures/heroes-of-goo-jit-zu [QSD,R=301,L]

The QSD (Query String Discard) flag removes the original query string from the redirect response.

I've included the end-of-string anchor ($) on the RewriteRule pattern, although strictly speaking it should be omitted to be equivalent to your original Redirect directive (which is prefix-matching).

You will need to clear your browser cache before testing since the earlier 301 will have been cached. (Test first with 302 - temporary - redirects to avoid potential caching issues.)

You are also missing the L flag on all your other rules - this should be added. At the very least, this prevents unnecessary processing of later rules, but with the L flag missing on the canonical redirect, this could result in an erroneous redirect, exposing the rewritten URL.

UPDATE: To clarify the discussion in comments regarding the QSA flag. This has nothing to do with your immediate issue. You will need the QSA flag if you are expecting a query string to be past on the requested URLs (in order to merge the query string with that stated in the substitution string), otherwise, this flag should be removed.

I have tried various flags on the end, both [QSA] and [QSD] cause 500 errors

The flags parameter is only available to mod_rewrite (RewriteRule and RewriteCond directives) - this is not valid on mod_alias Redirect (and RedirectMatch) directive(s).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文