简体   繁体   中英

301 htaccess Redirect that will force?

Trying this again because I realize how very poorly I worded my original post.

I have a page, Product1.php, that is dynamic for populating my products from a database.

This is the rubric of my SEO-friendly urls:

http://www.example/category/subcategory/model_name-model_id/product_id

This is the original url with query string that it rewrites to:

http://www.example.com/Product1.php?=Product_ID=1

This is the rewrite function I have in htaccess that makes this happen:

RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteRule ^category/.*/.*/([0-9]+)$ Product1.php?Product_ID=$1 [L,QSA]

However, if a user manually-types in http://www.example.com/Product1.php?=Product_ID=1 in the address bar, this is the one they still see. Moreover, it's the same for the search engine bots, which is dividing the value of my pages.

How do I write a 301 redirect that will force the user and bot to see the SEO-friendly url only , regardless of how they access the page? I have researched for days and various solutions I have tried give me only 404 or 500 errors.

Please help. Thanks in advance.

**EDIT: OK, looks like I can't invoke RewriteMap because I don't have access to my host's config files. (Need to upgrade our account to do so and employer is unwilling.) So will have to do a Rewrite Rule for each individual page, which is unfortunate but doable.

But still need to find out how to force the redirect with causing 404 or 500 errors. Anyone?

I used a little trick adding an "Internal" parameter to a rule responsible for main rewrite, and the check of this parameter in the 301 redirect. The solution with environment variables is also allows to process requests where the parameters may be presented in any order.

RewriteEngine on

# perform collecting parameters from query string to an environment variables

RewriteCond %{QUERY_STRING} Category=([^&]+)
RewriteRule . - [E=category:%1]

RewriteCond %{QUERY_STRING} SubCategory=([^&]+)
RewriteRule . - [E=subcategory:%1]

RewriteCond %{QUERY_STRING} Product_ID=([^&]+)
RewriteRule . - [E=productid:%1]

#...etc...

# give a right 301 redirect
RewriteCond %{REQUEST_URI} Product1.php
# make sure it's not an internal redirect
RewriteCond %{QUERY_STRING} !^Internal=1(.*)$
RewriteRule . /%{ENV:category}/%{ENV:subcategory}/%{ENV:productid}/? [L,R=301]

# now rewrite rule with the additional "Internal" parameter to label it as an internal redirect
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} ([^/]+)/([^/]+)/([^/]+)
RewriteRule .* Product1.php?Internal=1&Category=%1&SubCategory=%2&Product_ID=%3 [L]

Thus, the following request

/Products1.php?Category=cat1&SubCategory=subcat1&Product_ID=321

will result in 301 redirect to

/cat1/subcat1/321/

and the appropriate page will be shown.

One more way is to remove the "Internal" trick and use another name of the script, Products2.php for example. Ie you need something to distinguish external request from internal rewrite. to avoid infinite loop ("too much redirects" error).

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