简体   繁体   中英

.htaccess redirect based on a part of URL

After site crash a redirect php script doesn't work as expected. We try to fix it, but in the meantime we are looking for a quick solution to redirect search engine results so our visitors can at least visit after clicking a relative web page.

The url structure or the search engines result are something like this:

https://www.example.com/MainCategory/SubCategory_1/SubCategory_2/Product?page=1

and I'd like to redirect using the "SubCategory_2" part of the URL to something like this

https://www.example.com/SubCategory_2.php

so until we fully repair the script at least our visitors will se a relative web page.

I'm quite stuck... Any ideas? Thank you

To redirect the stated URL, where all parts are variable (including an entirely variable, but present query string) then you can do something like the following using mod_rewrite near the top of your root .htaccess file (or crucially, before any existing internal rewrites):

RewriteEngine On

RewriteCond %{QUERY_STRING} .
RewriteRule ^[^/]+/[^/]+/([^/]+)/[^/]+$ /$1.php [QSD,R=302,L]

The QSD flag is necessary to discard the original query string from the redirected response.

The above will redirect:

  • /MainCategory/SubCategory_1/SubCategory_2/Product?page=1 to /SubCategory_2.php
  • /foo/bar/baz/qux?something to /baz.php

You can test it here using this htaccess tester .


UPDATE:

unfortunately without success. I get 404 error.

You'll get a 404 if the directive did not match the requested URL, or /SubCategory_2.php does not exist.

Is the URL redirected? What do you see in the browser's address bar?

If there was no redirect then the above rule did not match the requested URL and the rule did nothing. Either because:

  • The URL format is not as stated in the question.
  • The rule is in the wrong place in the .htaccess file. As stated, this rule needs to be near the top of the config file.

I found a basic solution here htaccess redirect if URL contains a certain string I crate something like this RewriteRule ^(.*)SubCategory_2(.*)$ https://example.com/SubCategory_2.php[L,R=301] and works just fine. My problem is that this is a "static solution" since "SubCategory_2" is a variable.

Ok, but that is a very generic (arguably "too generic") solution for the problem you appear to be attempting to solve. This matches "SubCategory_2" anywhere in the URL-path (not just whole path segments) and preserves any query string (present on your example URL) through the redirect. So, this would not perform the stated redirect on the example URL in your question.

However, the directive you've posted (which you say "works just fine") cannot possibly work as written, at least not by itself. Ignoring the missing space (a typo I assume) before the flags argument, this would result in an endless redirect loop, since the target URL /SubCategory_2.php also matches the regex ^(.*)SubCategory_2(.*)$ .

Also, should this be a 301 (permanent) redirect? You seem to imply this is a "temporary" solution?

HOWEVER, it's not technically possible to make "SubCategory_2" entirely variable in this "basic solution" and search for this variable "something" in a larger string and redirect to "something.php". How do you know that you have found the correct part of a much larger URL? You need to be more specific about what you are searching for.

In your original question you are extracting the 3rd path segment in a URL-path that consists of 4 path segments and a query string. That is a perfectly reasonable pattern, but you can't extract "something" when you don't know what "something" is.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM