Forum Moderators: phranque

Message Too Old, No Replies

Trying to create clean url's from query strings

Can get the page to open but old messy url stays in address bar

         

JWJonline

7:06 pm on Oct 9, 2020 (gmt 0)

10+ Year Member



I have an art site and am in the process of making it work with clean url's using htaccess Rewrites. Straightforward pages are working such as www.example.com/page.php rewriting to www.example.com/page. Where I am having trouble is in trying to clean up www.Example.com/gallerypage.php?autoload=nameofpicture to www.Example.com/gallerypage/nameofpicture. I built the Gallery using Highslide many years ago but have only recently started using the "autoload" feature to open the named image on page load.

The main issue I am having is that the messy url shows in the address bar. This happens when I use
RewriteRule gallerypage/(.*)$ https://www.example.com/gallerypage.php?autoload=$1
though the image does open correctly. For reasons that I don't understand, if I remove the domain name and use
RewriteRule gallerypage/(.*)$ gallerypage.php?autoload=$1
then the clean url is shown in the address bar, but the image doesn't open.

I feel as if I'm very close but can't see what I'm missing. I'll be very grateful for any help. Thank you.

lucy24

8:12 pm on Oct 9, 2020 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Insert “I really hate this damned machine” boilerplate.

RewriteRule gallerypage/(.*)$ https://www.example.com/gallerypage.php?autoload=$1
This rule redirects FROM a short pretty URL, TO a long messy one. At least, it does if there's an [R=301,L] flag that you forgot to include when posting. If not, all kinds of things can happen; at best it will be a 302 temporary redirect, but there are worse possibilities. Oh, and the $ anchor in this specific rule is superfluous. It does no harm, but only because it doesn't do anything at all.

The second rule
RewriteRule gallerypage/(.*)$ gallerypage.php?autoload=$1
is again missing an [L] flag, and now I begin to suspect it really is missing, which is a problem. The target in any internal rewrite ought to begin with / for root, but this is generally a non-lethal error. Is the image--or the code for opening it--in fact located at /gallerypage.php, which in turn is located at the root level? In fact, what is gallerypage.php? is it simply the php script that displays the picture?

Incidentally, if the request begins in /gallerypage (i.e. example.com/gallerypage) you should express the pattern as
^gallerypage
with opening anchor. This too is a non-lethal error; it's just more efficient for the server.

w3dk

8:25 pm on Oct 9, 2020 (gmt 0)

10+ Year Member Top Contributors Of The Month



You'll need to make sure that MultiViews is disabled, otherwise, mod_negotiation will trigger a subrequest for "gallerypage.php" without the "autoload" parameter. (Because your "pretty" URL is the same as the underlying file basename: "gallerypage".)

In other words, add the following to the top of your .htaccess file:


Options -MultiViews

JWJonline

9:31 am on Oct 10, 2020 (gmt 0)

10+ Year Member



Thank you very much Lucy24. My situation is worse than I thought it was and I think I'm out of my depth with this. You are right that /gallerypage.php is in the root and it contains a call to javascript code (Highslide) to open the images. /gallerypage.php is just a regular web page with multiple <divs> containing the images. I've tweaked my code to include ^ and [L] but it still works the same, in other words, the first code opens the image but makes the url messy and the second code retains the pretty url but doesn't open the image.

And thank you w3dk, I've added that near the top of my file.

lucy24

3:56 pm on Oct 10, 2020 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I tend to think you really want two rules, which will be located in different parts of your htaccess.

On one side is the redirect to handle explicit requests for "gallerypage.php"
RewriteCond %{THE_REQUEST} \.php
RewriteCond %{QUERY_STRING} autoload=([\w-]+)
RewriteRule ^gallerypage\.php https://example.com/gallerypage/%1 [R=301,L]
Theoretically you could merge both conditions, with a single long THE_REQUEST including the capture, but then you have to worry about other parameters sneaking in, and it might get messy.

On the other side is the internal rewrite
RewriteRule ^gallerypage/(.+) /gallerypage.php?autoload=$1 [L]
I think technically you don't even need to pass the parameter, because the php page can look up the URI for itself (because it's an internal rewrite, not a redirect), but it feels safer to include it.

Edit: If the rewrite doesn't result in the image being displayed as intended, you may need to wander next door to the php subforum and find out what's missing.

JWJonline

10:11 am on Oct 12, 2020 (gmt 0)

10+ Year Member



Thank you very much lucy24. The image isn't displaying as intended but the 'pretty' url's are now working perfectly. I'd have never gotten this far on my own so I'm very grateful. Off to dig into the php.