Rectangle 27 0

php Formatting .htaccess RewriteCond RewriteRule for removing query string from URLs?

$subject = substr($uri, strrpos($, "/"));
RewriteEngine on
RewriteBase /

RewriteCond %{QUERY_STRING} !no-redir [NC]
RewriteCond %{QUERY_STRING} (^|&)subject=([^&]+) [NC]
RewriteRule ^(.*)/product.php$ $1/%2? [NC]

RewriteRule ^([^+\s]+)(?:[+\s]+)([^+\s]+)((?:[+\s]+).*)$ $1-$2$3 [DPI,N]
RewriteRule ^([^+\s]+)(?:[+\s]+)([^+\s]+)$ $1-$2 [R=301,DPI,L]
RewriteRule ^(.*/popcorn)/([^/.]+)$ $1/product.php?subject=$2&no-redir [NC,L]

@Chris, those were Java equivalents of PHP strrpos() and substr(). Please, have a look at my update. That's the change I'm proposing for your landing php page.

Add the following after the rules defined above.

Assuming your .htaccess is in the web root / directory

Chris, the idea is to now capture the subject from path info instead of a GET parameter because it's not being passed as ?subject=sub+info which makes it unavailable as $_GET['subject'] now.

Notice, I've added another RewriteCond on %{QUERY_STRING} above. This also means you can revert your product.php to the way it was before since subject is being passed again as usual.

Ravi, I was able to utilize the $_SERVER['REQUEST_URI'] variable to satisfy the conditional

Thanks for the help Ravi, is 'lastIndexOf"/" with sub-string' a JavaScript function? I am not familiar with a PHP function of that name. If you could provide any direction or links on how to use that technique I would be very appreciative.

That code worked perfectly to reformat the URL's, however I am now getting a 404 Server Error. The landing page has a conditional to check if the $subject variable isset before the page loads - would the reformatted URL (without the 'subject=') affect the conditional?

Yes, it would. The landing page won't recognize subject unless passed as ?subject=. I think formatting the url as /shop/popcorn/?subject=Bacon-and-Cheddar-Popcorn should work. Your call. Shorten it to just s if you like or pass as /?Bacon-and... Your php would have to accommodate accordingly. I would suggest you keep the pretty url and use lastIndexOf "/" with sub-string to capture the subject.

You'd need to get the subject keywords from the URI path as follows: