Friendly URLs help

Hi all. I’d like to create friendly URLs for my pages but do not know enough about htaccess. I’d like the following pages (of which there are 100,000s of thousands of combinations) to go directly to /compare.php to be processed:

/wearables/apple-watch-vs-fitbit-surge-vs-jawbone-up3/

The emboldened part is the dynamic part.

I’d also like the following pages to go directly to /product.php

/smartwatch/apple-watch/
/fitness-tracker/fitbit-surge/

Thank you!

Have you searched, tried a few examples and encountered problems?

Actually just got it! I don’t understand all of this but let’s hope it doesn’t cause any problems!

Options +FollowSymLinks RewriteEngine On RewriteCond %{SCRIPT_FILENAME} !-d RewriteCond %{SCRIPT_FILENAME} !-f RewriteRule ^(.*)$ http://comparewear.com/$1 [R=301] RewriteRule ^smartwatch/(.*)$ ./product_new.php [L] RewriteRule ^fitness-tracker/(.*)$ ./product_new.php [L]

1 Like

That’s why we’re here :slight_smile:

Okay. I managed to figure out all of the URL rewriting, and everything is working perfectly at http://comparewear.com

HOWEVER, I’m trying to 301 redirect some of my pages that are indexed in Google as /product.php?query and /compare.php?query

I’m encountering the most frustrating problem:

if(strpos($_SERVER['REQUEST_URI'], '.php?vs=') !== false) { // some code to generate $URL_friendly }

// echo 'Location: http://comparewear.com/'.$URL_friendly; die(); header("HTTP/1.1 301 Moved Permanently"); header('Location: http://comparewear.com/'.$URL_friendly);

When I echo $URLfriendly, the location appears fine. I can even copy/paste it into the address bar of my browser, and it displays perfectly. But when I keep the line commented and allow the header redirect, I get a 404 error. How is this happening?

In case my htaccess has anything to do with it:[code]ErrorDocument 400 /error.php?code=400
ErrorDocument 401 /error.php?code=401
ErrorDocument 403 /error.php?code=403
ErrorDocument 404 /error.php?code=404
ErrorDocument 500 /error.php?code=500

Options +FollowSymLinks RewriteEngine On RewriteCond %{HTTP_HOST} www.comparewear.com$ RewriteCond %{SCRIPT_FILENAME} !-d RewriteCond %{SCRIPT_FILENAME} !-f RewriteRule ^(.*)$ http://comparewear.com/$1 [R=301] RewriteRule ^smartwatches/(.*)$ ./product.php [L] RewriteRule ^fitness-trackers/(.*)$ ./product.php [L] RewriteRule ^compare/(.*)$ ./compare.php [L] [/code]

Not even this works!header('Location: http://google.com');

Oh my. This is so odd. I tried everything I could think of, and then finally during some testing I added echo ‘test’; die(); after header(), and suddenly it worked. I realized that I needed to specify header(); die(); !! That was it!

Why do I need to specify die() after a header() redirect? Does the browser not immediately leave the current page when it receives the header command? If not, should all header redirects be followed by die() commands as good practice?

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.