Jump to content

Another mod_rewrite not working


SaranacLake

Recommended Posts

Getting my butt kicked today by Apache!  😠

When a user enters this URL...

www.mydomain.com/client1/gallery/2019-holiday-party

I want my mod_rewrite to go here...

www.mydomain.com/client1/gallery/photo-gallery.php?gallery-id=2019-holiday-party

Here is what I have...

	RewriteCond %{REQUEST_FILENAME} !-f
	RewriteRule client1/gallery/(.*)$ client1/gallery/photo-gallery.php?gallery-id=$1 [L,R=301]
	

 

When I click on this hyperlink...

	<a href="/client1/gallery/2019-holiday-party">Holiday Party (2019)</a>
	

 

...my browser is going here...

	http://www.mydomain.com/client1/gallery/photo-gallery.php?gallery-id=2019-holiday-party
	

 

When clicking on the above link, I do want to go to the above URL *location* but I want to see: http://www.mydomain.com/client1/gallery/2019-holiday-party in the browser Address Bar.

 

The code above is simply a tweak from some earlier code that I wrote for another website that worked fine.

 

What did I do wrong?? 

 

Edited by SaranacLake
Link to comment
Share on other sites

I guess I should add that i have this file structure...

	public_html
	public_html > index.php
	 
	public_html > client1
	public_html > client1 > menu.php
	 
	public_html > client1 > gallery
	public_html > client1 > gallery > photo-gallery.php
	public_html > client1 > gallery > 2019-holiday-party
	public_html > client1 > gallery > 2019-holiday-party > IMG_0001.jpg
	

Link to comment
Share on other sites

12 minutes ago, requinix said:

Do you know what the [R] flag to RewriteRule means?

To be honest, I am very rusty on mod_rewrites and was relying on old ones being done properly and hoping my tweaks would be enough to make these fit my new situation.

I believe "R" is for "redirect" and "L" is for "last" as in don't run any more statements after this.

Link to comment
Share on other sites

4 minutes ago, requinix said:

Right so far.

So if [R] means redirect...

I'm not sure...

All I know is I have about a dozen complex mod_rewrite for another site I wrote that all work fine, and I tried to find ones similar to my needs for this photo site and modify them.

I won't claim I truly understand mod_rewrites in and out, although I do like to learn.

My understanding of things work is that you take a "pretty" URL like client1/gallery/2019-holiday-party and you rewrite it to something that Apache will recognixe like client1/gallery/photo-gallery.php?gallery-id=2019-holiday-party and then I *think* you tell Apache to redirect to that "real" URL but your browser still shows the "pretty" URL and then it is also a good idea to tell Apache you are done with [L].

How does that sounds?

 

Link to comment
Share on other sites

Not quite. mod_rewrite will rewrite and "tell" Apache about the new one automatically. That's its whole point. Base functionality. You don't need [R] to do that.

What [R] means is "figure out the new URL and send it to the client with a 3xx redirect".

Link to comment
Share on other sites

2 minutes ago, requinix said:

Not quite. mod_rewrite will rewrite and "tell" Apache about the new one automatically. That's its whole point. Base functionality. You don't need [R] to do that.

What [R] means is "figure out the new URL and send it to the client with a 3xx redirect".

So you should never use [R] since it is bad since it cause 3X redirects?

Anywho, what about the crux of what I last wrote...

My understanding of things work is that you take a "pretty" URL like client1/gallery/2019-holiday-party and you rewrite it to something that Apache will recognixe like client1/gallery/photo-gallery.php?gallery-id=2019-holiday-party and then I *think* you tell Apache to redirect to that "real" URL but your browser still shows the "pretty" URL

Do you agree with everything before the redirect part?

 

Link to comment
Share on other sites

3 minutes ago, SaranacLake said:

So you should never use [R] since it is bad since it cause 3X redirects?

It's not bad. It just isn't what you want to do.

At least in this thread. In your other thread, you do want the [R] on that menu.php rule because you want the user to see the /whatever/ URL. I assume.

3 minutes ago, SaranacLake said:

Anywho, what about the crux of what I last wrote...

The rest of the stuff you said was right.

Link to comment
Share on other sites

3 minutes ago, requinix said:

It's not bad. It just isn't what you want to do.

At least in this thread. In your other thread, you do want the [R] on that menu.php rule because you want the user to see the /whatever/ URL. I assume.

The rest of the stuff you said was right.

 

So if my mod_rewrite is correct, then why is it not working??  

Please re-read my OP.

The mod_write seems to be working in that the pretty URL www.mysite.com/client1/gallery/2019-holiday-party is being redirected to www.mysite.com/pnc/gallery/photo-gallery.php?gallery-id-2019-holiday-party HOWEVER the redirected "ugly" URL is appearing in the address bar.

Link to comment
Share on other sites

6 minutes ago, SaranacLake said:

So if my mod_rewrite is correct, then why is it not working??  

I never said it was correct. I said your understanding of how the process works was correct - at least up to the part about redirecting. I then corrected you on how it works when you do and don't include the [R].

So you should now understand why what you have is not working the way you want, and what you need to do to fix it.

Link to comment
Share on other sites

33 minutes ago, requinix said:

I never said it was correct. I said your understanding of how the process works was correct - at least up to the part about redirecting. I then corrected you on how it works when you do and don't include the [R].

So you should now understand why what you have is not working the way you want, and what you need to do to fix it.

I missed your reply on this more pressing issue...

 

I'm not entirely sure I understood your lesson and the correct answer.  Please help me with this one!

 

It's not bad. It just isn't what you want to do.

At least in this thread. In your other thread, you do want the [R] on that menu.php rule because you want the user to see the /whatever/ URL. I assume.

In my first thread, I want to go from /client/menu to /client/menu.php so I am adding to things, and you are saying I do want a redirect here because I want to go to the script menu.php?

Correct?

 

Then in this thread, I want to go from /client1/gallery/2019-holiday-party to /client1/gallery/photo-gallery.php?gallery-id=2019-holiday-party where here I am not adding or deleting but creating a new URL, and here I would also think I want to redirect to the script "photo-gallery.php", right???

You make it sound like my use of [L,R=301] is breaking things?  Is that true?  And if so, please help me udnerstand what I need to do to fix things!

Again, my this 2nd mod_rewrite is sending me to the correct ugly URL and script, but I do NOT want to see the ugly script when the browser redirects to "photo-gallery.php".  INSTEAD, I want the user to still see the pretty URL.

So how do I make that happen?

 

Link to comment
Share on other sites

@requinix,

 

P.S.

If I remove [L,R=301] from my mod_rewrite, I get this funkiness...

When my browser redirects, the URL is an amalgamation of things like this...

 

/client/gallery/photo-gallery.php?gallery-id=2019-holiday-party

 

What is going on here?

 

The first half looks good /client/gallery but what is happening with the rest photo-gallery.php?gallery-id=2019-holiday-party  ???

 

Again, I am expecting to see this in the address bar:  /client1/gallery/2019-holiday-party

Link to comment
Share on other sites

12 minutes ago, requinix said:

I don't understand how you could possibly be confused about what the URL is.

Your browser seems to be remembering the redirect. Try an incognito/private window, or clear your cache.

Scratch my P.S. message above - I was on a confernce call at work and misread things.

 

However, in my previous message I asked why my pretty URL is not remaining in the address bar.

 

And you are making me work extra hard to figure out this whole {L,R=301] thing!  Show some mercy!!  😉

 

Link to comment
Share on other sites

3 minutes ago, SaranacLake said:

However, in my previous message I asked why my pretty URL is not remaining in the address bar.

Let me summarize what we covered in the first few posts:

1. The URL in the address bar is changing
2. [R] tells mod_rewrite that it should tell Apache that it should tell the browser to redirect to the new URL
3. You have a [R] in that rule

I can't make it any more clear than that without actually giving away the answer.

Link to comment
Share on other sites

16 minutes ago, requinix said:

Let me summarize what we covered in the first few posts:

1. The URL in the address bar is changing
2. [R] tells mod_rewrite that it should tell Apache that it should tell the browser to redirect to the new URL
3. You have a [R] in that rule

I can't make it any more clear than that without actually giving away the answer.

 

Sorry, have been multi-tasking.

I am retracting my earlier comment, and ask you to look at this again...

 

****************************************************

SCENARIO #1:

In this scenario I have...

	RewriteCond %{REQUEST_FILENAME} !-f
	RewriteRule client1/gallery/(.*)$ client1/gallery/photo-gallery.php?gallery-id=$1 [L,R=301]
	

 

When I run this, it goes to the correct photo-gallery.php page HOWEVER, the URL is NOT correct...

www.mysite.com/client1/gallery/photo-gallery.php?gallery-id=2019-holiday-party

It should be...

www.mysite.com/client1/gallery/2019-holiday-party

 

****************************************************

SCENARIO #2:

In this scenario I have...

 

	RewriteCond %{REQUEST_FILENAME} !-f
	RewriteRule client1/gallery/(.*)$ client1/gallery/photo-gallery.php?gallery-id=$1 
	

 

 

When I run this, it goes to the correct photo-gallery.php page HOWEVER, the URL is NOT correct...

www.mysite.com/client1/gallery/2019-holiday-party/?gallery-id=2019-holiday-party

It should be...

www.mysite.com/client1/gallery/2019-holiday-party

 

In either scenario, with or without a [L,R=301] my pretty URL is getting screwed up!!  WHY??

 

Edited by SaranacLake
Link to comment
Share on other sites

2 minutes ago, requinix said:

Do not put the gallery-id thing into your links.


<a href="/client1/gallery/2019-holiday-party">

 

But "2019-holiday-party" is part of the pretty URL.  If I take that out then I get a "Page Not Found" error because my variable will be blank as seen below...

	RewriteCond %{REQUEST_FILENAME} !-f RewriteRule client1/gallery/(.*)$ client1/gallery/photo-gallery.php?gallery-id=$1 
	

 

Hopefully you picked up on the difference highlighted in red in my last post??

 

By adding back in [L,R=301] at least thing load properly, but then all f that aggravation to create a mod_rewrite to allow a pretty URl was for not?!

I thought I was pretty good with mod_rewrites, but I have spent all day staring at my code and trying different things and re-reading my code and I just can't figure out what the problem is, because I swear what I have looks right?!  :shrug:

Link to comment
Share on other sites

I'm tired.

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule client1/gallery/(.*)$ client1/gallery/photo-gallery.php?gallery-id=$1 [L,R=301]

That was your original code. Remove the [R] flag and only the [R] flag.

Then make your link look like what I put in my last post.

Link to comment
Share on other sites

1 minute ago, requinix said:

I'm tired.


RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule client1/gallery/(.*)$ client1/gallery/photo-gallery.php?gallery-id=$1 [L,R=301]

That was your original code. Remove the [R] flag and only the [R] flag.

Then make your link look like what I put in my last post.

 

Removed: R=301

Changed link to: <a href="client1/gallery/">

 

===> Page Not Found

 

Link to comment
Share on other sites

15 minutes ago, requinix said:

o rly?

Oh really?

Yes.

I don't know as much as you, but I can say with certainty that my links need to be "fully formed" and have all 3 components:

client1 directory

gallery directory

2019-holiday-party which is an argument passed to the $galleryID parameter/variable in my code used to serve up the apropriate gallery.

 

Link to comment
Share on other sites

19 hours ago, SaranacLake said:

Changed link to: <a href="client1/gallery/">

That link is relative to the current URL.  So if you're say on https://example.com/client1/menu, clicking that link will attempt to load https://example.com/client1/menu/client1/gallery

When you're rewriting your URLs you need to take into account your URLs may not match up with what your filesystem looks like and make sure you link accordingly.  In general it's almost always best to link things in a domain relative fashion, with a leading /.

<a href="/client1/gallery/2019-holiday-party">2019 Holiday Party</a>

Also, in case you're still not clear on the [R] issue: A normal RewriteRule without the [R] tells Apache "Hey buddy, I know the client asked for X but let's really give them Y instead, don't tell them about it though."

If you add the [R], that changes the hypothetical conversation to "Hey buddy, I know the client asked for X but they really want to ask for Y.  Can you please let them know they need to request Y instead?"

By adding the 301 into the mix ([R=301]), your modifying the conversation even further to say "Hey buddy, I know the client asked for X but they really want to ask for Y.  Can you please let them know they need to request Y instead and that this will always be the case and they should never request X again?"

Browsers may cache such 301 redirects so that if you attempt to visit the original URL again the browser itself will re-write to the redirected URL before even making the request.  As such, if you make such a redirect by mistake (as you were) it's not easily fixed.  Sometimes closing the browser entirely (not just the tab/window) will clear those redirects, other times you may  have to clear the browsers cache to remove them.

 

Link to comment
Share on other sites

@kicken,

Thanks for the response.  Am at work and getting ready for a meeting, but let me try and get my head back into this quickly...

 

1 hour ago, kicken said:

That link is relative to the current URL.  So if you're say on https://example.com/client1/menu, clicking that link will attempt to load https://example.com/client1/menu/client1/gallery

When you're rewriting your URLs you need to take into account your URLs may not match up with what your filesystem looks like and make sure you link accordingly.  In general it's almost always best to link things in a domain relative fashion, with a leading /.


<a href="/client1/gallery/2019-holiday-party">2019 Holiday Party</a>

 

Let say that I am on menu.php and I cick on one of the pictures/links to choose a gallery...

The location of menu.php is: public_html/client1/menu.php

The URL for menu.php is: http://local.mydomain/client1/menu

And in menu.php I have this HTML code...

	<!-- MENU -->
	<a href="/client1/gallery/2019-holiday-party">Holiday Party (2019)</a>
	<a href="">other gallery links to follow</a>
	

So I believe I was using an absolute reference like you suggest.

When I click on that first link, I want it to launch the gallery for the "Holiday Party (2019)".

In my mod_rewrite, I have this to allow a pretty URL when requesting the gallery:

	RewriteCond %{REQUEST_FILENAME} !-f
	RewriteRule client1/gallery/(.*)$ client1/gallery/photo-gallery.php?gallery-id=$1 [L,R=301]
	

 

So the pretty URL from the hyperlink above should yield: http://local.mydomain/client1/gallery/2019-holiday-party

And this should map to (and serve up) the ugly URL: http://local.mydomain/client1/gallery/photo-gallery.php?gallery-id=2019-holiday-party

 

HOWEVER, as I mentioned a couple times above to @requinix...

Scenario #1:

If I have [L,R=301] in my mod_rewrite then I get...

http://local.mydomain/client1/gallery/photo-gallery.php?galery-id=2019-holiday-party

(That is the correct end location/file/query string, BUT it is the ugly URL which I clearly do NOT want!!)

 

Scenario #2:

If I remove [L,R=301] in my mod_rewrite then I get...

http://local.mydomain/client1/gallery/2019-holiday-party/?gallery-id=2019-holiday-party

(The first part of the URL is what the pretty URL should be, but then there s oher junk on the end that is wrong?!)

 

So how do I fix this? 

I'm not sure if @requinix saw these *subtle* differences or understood why I'm not getting what I want.

And I'm sorry, but I don't see how [L] or [R] or [R=301] or [L,R=301] is related.

 

 

Edited by SaranacLake
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.