Htaccess: generates an error if a period (.) Exists in the query string

I wrote the .htaccess file as follows:

Options +FollowSymLinks RewriteEngine On #open the product details page with the Product Number with PN prefix RewriteRule ^((products/|product/|)PN[0-9-]+)/?$ product.php?pno=$1 #open the product search page for a particular category RewriteRule ^((bat|ref|acc)[A-Za-z0-9-]+)/?$ search.php?cat=$1 [NC] #open the product search page for a particular category RewriteCond %{REQUEST_FILENAME} !-f RewriteRule ^([A-Za-z0-9-_.,]+)/?$ search.php?search =$1 [NC] RewriteRule !\.(html|php)$ - [S=4] RewriteRule ^([^_]*)_([^_]*)_([^_]*)_([^_]*)_(.*)$ $1-$2-$3-$4-$5 [E=uscor:Yes] RewriteRule ^([^_]*)_([^_]*)_([^_]*)_(.*)$ $1-$2-$3-$4 [E=uscor:Yes] RewriteRule ^([^_]*)_([^_]*)_(.*)$ $1-$2-$3 [E=uscor:Yes] RewriteRule ^([^_]*)_(.*)$ $1-$2 [E=uscor:Yes] RewriteCond %{ENV:uscor} ^Yes$ 
  • It cannot accept if there is a period (.) In the line of the query string. Now it is resolved, but cannot accept its previous rule.

  • In addition, I want it to correctly rewrite "URL / product.php? Pno = PN123" if the URL "URL / products / PN123" or "URL / product / PN123" is specified. Please note that it can redirect correctly if "URL / PN123" is set correctly with the correct CSS. And "URL / product / PN123" can also retrieve data, but cannot show CSS in pre-order.

+4
source share
2 answers

This is your patched / updated .htaccess, however it would be better if I wrote more about your real requirements:

 Options +FollowSymLinks -MultiViews RewriteEngine On RewriteBase / #open the product details page with the Product Number with PN prefix RewriteCond %{REQUEST_FILENAME} !-f RewriteRule ^((products/|product/|)PN[0-9-]+)/?$ product.php?pno=$1 [L,NC,QSA] #open the product search page for a particular category RewriteCond %{REQUEST_FILENAME} !-f RewriteRule ^((bat|ref|acc)[A-Za-z0-9-]+)/?$ search.php?cat=$1 [L,NC,QSA] #open the product search page for a particular category RewriteCond %{REQUEST_FILENAME} !-f RewriteRule ^([A-Za-z0-9-_.,]+)/?$ search.php?search=$1 [L,NC,QSA] RewriteRule !\.(html|php)$ - [S=4,NC] RewriteRule ^([^_]*)_([^_]*)_([^_]*)_([^_]*)_(.*)$ $1-$2-$3-$4-$5 [E=uscor:Yes] RewriteRule ^([^_]*)_([^_]*)_([^_]*)_(.*)$ $1-$2-$3-$4 [E=uscor:Yes] RewriteRule ^([^_]*)_([^_]*)_(.*)$ $1-$2-$3 [E=uscor:Yes] RewriteRule ^([^_]*)_(.*)$ $1-$2 [E=uscor:Yes] RewriteCond %{ENV:uscor} ^Yes$ 

About your css, js, image file: make sure your path to css, js etc starts with a slash / , and not relative.

+3
source

You have some errors in your regular expressions. Upload this check to your website if you want to play with them.

  • Sample ^((products/|product/|)PN[0-9-]+)/ does not match "URL / products / PN123" or "URL / product / PN123" or "URL / PN123" and returns PN123 due to embedded string matching and trailing slash. You can hide the product (s) / and make trailing / optional, as shown below.

  • I do not like to use! prefix in rule templates, especially when using grouped parts. Too easy to shoot in the leg. I prefer cond followed by a null rule or a negative result.

  • Crash matching rules, though, may result in a postfix error, so I avoid them. The kernel starts re-launching the .htaccess files (because they are evaluated in the context of the "Per Directory") until a match occurs, so I find it LOT safer to always use the [L] flag
  • Again, because of this loop, you need to make sure your rules end, and it is best to do this in a simple transparent way. In this case, you do not seem to want to rewrite the URIs that map to real files, so why not take the -f test to the top, and all other rules can then assume that this condition is true.
  • Two search rules mean that the parameter cat = acc123 is used if the query for / acc 123 / etc and search = otherwise. Is this what you want?
  • You need to enable the QSA flag if you do not want to ignore any existing parameters.
  • The USCOR environment variable (I am capitalized) becomes REDIRECT_USCOR in the next pass, so this is what you are testing. I assume the rule follows the last cond

Introducing it all together:

 Options +FollowSymLinks -MultiViews RewriteEngine On RewriteBase / # The rewrite engine loops in a Per Dir context. We don't remap real files so: RewriteCond %{REQUEST_FILENAME} -f RewriteRule ^ - [L] # Open the product details page with the Product Number with PN prefix RewriteRule ^(?:products?)?(PN\d+)/?$ product.php?pno=$1 [L,NC,QSA] # Open the product search page for a particular category RewriteRule ^((?:bat|ref|acc)[A-Za-z0-9-]+)/?$ search.php?cat=$1 [L,NC,QSA] #open the product search page for a particular category RewriteRule ^([A-Za-z0-9-_.,]+)/?$ search.php?search=$1 [L,NC,QSA] RewriteRule \.(?!(php|html)$) - [S=4,NC] RewriteRule ^(.*?)_(.*?)_(.*?)_(.*?)_(.*)$ $1-$2-$3-$4-$5 [E=USCOR:Yes, L] RewriteRule ^(.*?)_(.*?)_(.*?)_(.*)$ $1-$2-$3-$4 [E=USCOR:Yes, L] RewriteRule ^(.*?)_(.*?)_(.*)$ $1-$2-$3 [E=USCOR:Yes, L] RewriteRule ^(.*?)_(.*)$ $1-$2 [E=USCOR:Yes, L] 

I'm not sure what you want to do with% {ENV: REDIRECT_USCOR} now without additional rules and logic, although it will be available in a script like $_SERVER["REDIRECT_USCOR'] .

+2
source

Source: https://habr.com/ru/post/1396848/


All Articles