Pretty URL Question for htaccess file

I am a beginner level coder and I am trying to learn about clean URLs and htaccess files.

I want to create a rule that will only execute if “/gallery” is contained within the URL to be linked, and will change:

www.example.com/index.php?targetDisplay=gallery&targetLine=product-line&targetSet=product-name

to

www.example.com/gallery/product-line/product-name

I have reviewed a few examples, but I’m still a little confused.

Would the following be the correct code?

RewriteRule ^gallery/([0-9a-zA-Z_-]+)/([0-9a-zA-Z_-]+) index.php?targetDisplay=gallery?targetLine=$1$title=$2 [NC, L]

Or does the name after the “^” symbol, have to match the site name (i.e. ^index and index.php ?)

I have been trying some basic experiments and can’t get this to work at all:

RewriteEngine on
RewriteRule ^custom index99.php [NC,L]

If I understand this correctly, I should be able to type: www.example.com/custom and should be linked to www.index99.php but instead I get a 404 Not Found error. Any ideas on what might be wrong?

Presumably you mean that you should be linked to www.example.com/index99.php, yes? Yes, that is what it should do.

Thank you Tracknut, it looks like it was not working because of other rules my webhost had in the file:

# RewriteCond %{HTTP_HOST} mysite\.com [NC]
# RewriteCond %{SERVER_PORT} 80
# RewriteCond %{REQUEST_URI} !^/\.well-known/acme-challenge/[0-9a-zA-Z_-]+$
# RewriteCond %{REQUEST_URI} !^/\.well-known/cpanel-dcv/[0-9a-zA-Z_-]+$
# RewriteCond %{REQUEST_URI} !^/\.well-known/pki-validation/[A-F0-9]{32}\.txt(?:\ Comodo\ DCV)?$
# RewriteRule ^(.*)$ https://mysite.com/$1 [R,L]

I have no idea what these rules do or if they are vital to the website, but once I commented them out, then this test example worked.

The [L] flag says this is the last rule in the rule set, and no further rules should be processed. It could be that just removing it from their last [R,L] will let yours run.

Thank you again.

Off Topic:

Please use example.com for example URLs, and not mysite.com, which is a real site.

Thank you.

Will do. Thank you.

1 Like

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