How do I rewrire this link?

Hi Everyone…

Hope you can help me with this…
I would like to rewrite this link

http://www.rajeevthomas.com/viewgallery.php?cid=6&pid=392

to this

http://www.rajeevthomas.com/viewgallery/6/392

Do I rewrite that like this?

RewriteRule ^viewgallery/([0-9]+)/([A-Za-z0-9-]+)?$ viewgallery.php?cid=$1&pid=$392 [NC,L]

Am I doing this right?

Thanks for any help…

Hi yath!

You were close … see edits of your code below:

Comments:

  1. Be sure that you have Options -MultiViews in your .htaccess file. If not, MultiViews will allow the viewgallery directory to be hijacked to the viewgallery.php script and you will not like the consequences.

  2. Your example required that the cid value be supplied (as digit(s)) so I eliminated the letters and the ? which made the cid value optional.

  3. Because the URI is case sensitive, you do NOT want to use the No Case flag!

You might benefit from reading the mod_rewrite tutorial linked in my signature as it contains explanations and sample code. It’s helped may members and should help you, too.

Regards,

DK

dklynn… Thank you for your answer. Every word you said is a learning experience!. I have to look them up quite a bit! After going through your tutorials I realized that I need to use RewriteCond and use redirect in my links. I am not sure how to do it quite yet. But I tried to do it…can you please help me with it…

Currently my link structure is this…

But I would like the viewer to see this link xyz.com/viewgallery/Colorado-Fall/Touched-By-Light ( This means redirect?)

Then rewrite back to my link. ( ?? This is how I understand it)…

So I wrote…

check if the actual request if for “this1”

RewriteCond %{THE_REQUEST} [1]{3,9}\ /viewgallery.php\?cname=(.*)&pcaption=(.+)

redirect to “this2”

RewriteRule ^viewgallery\.php /%1/%2/?%3 [R=301,L]

now rewrite “this2” back to “this1”

RewriteRule ^(.*?)/(.+)$ /viewgallery.php?cname=$1&pcaption=$2 [L,QSA]

Is this correct? I am sure it looks like gibberish …sorry about that!..

Thanks for your help…


  1. A-Z ↩︎

I finally got this …I think this is the right one but it is still not working… is makes the large image itself disappear…

Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /

RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\\s/+(viewgallery)\\.php\\?cname=([^&]+)&pcaption=([^&\\s]+) [NC]
RewriteRule ^ /%1/%2/%3? [L,R=301,NE]

RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/?$ /$1.php?cname=$2&pcaption=$3 [L,QSA]

What am I doing wrong?


  1. A-Z ↩︎

Hi yath!

Since I’m not familiar with either your design or your requirements, I can only comment on your code (and request that you use the [noparse]

 ... 

[/noparse] wrapper for your code to prevent it from disappearing from the quoted response):

Options [COLOR="#808080"]+FollowSymLinks[/COLOR] -MultiViews 
# FollowSymLinks should already be in the httpd.conf, i.e., superfluous
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
# This is dangerous but only because it can redirect to the DocumentRoot without your intending to do so

# redirect ORIGINAL request FROM the serveable version to the /'d version
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\\s/+(viewgallery)\\.php\\?cname=([^&]+)&pcaption=([^&\\s]+) [NC]
RewriteRule ^ /%1/%2/%3? [L,R=301,NE]

RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/?$ [COLOR="#808080"]/[/COLOR]$1.php?cname=$2&pcaption=$3 [L,QSA]
# / is not necessary and could cause problems
# BECAUSE Apache will first go to the server root to look for $2/$3 then your DocumentRoot

Regards,

DK

Thank you DK…thank you for explaining everything. I have a question… do pretty URLs work only with absolute paths to images and directories?

Hi yath!

If your mod_rewrite code is changing the directory level (as seen by the visitors’ browsers - this is what you’re doing), then the answer is yes, otherwise, it’s not necessary.

For more information, I have a section of my signature’s tutorial Article entitled “Relative Links are Missing!” which should explain it better than the above sentence.

Regards,

DK

Thank you DK… your tutorials really helped. Right now I have the re-direct working. But I have a problem with images. Just .jpgs , won’t show up if they are in sub-folders. If they are placed in the root folder, it is all okay, they will show up. I can only get the images to show up in sub folders If I remove the .jpg extension!. This is what I have in .htaccess now… can you give me some directions please? Did I write the .htaccess code right?

Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
# /viewgallery.php?cname=Colorado-Fall&pcaption=Poked to /photos/Colorado-Fall/Poked.jpg
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\\s/+viewgallery\\.php\\?cname=([^&]+)&pcaption=([^&\\s]+) [NC]
RewriteRule ^ /photos/%1/%2.jpg? [R=302,L,NE]
RewriteRule ^photos/([^/]+)/([^.]+)\\.jpg$ /viewgallery.php?cname=$1&pcaption=$2 [QSA,L,NC,NE]

Hi Yath!

Sorry for the long delay on my part - out of town and quite ill.

I’m glad that you have found the tutorial useful, too!

Okay, a problem with images will normally be an indication that you’ve changed your directory level with a redirection (to relative links are displaced), however, your code shows something else:


Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
[color="#ff0000"]This, I have found, is unnecessary and can cause unintended problems if used.[/color]

[COLOR="#808080"]# /viewgallery.php?cname=Colorado-Fall&pcaption=Poked to /photos/Colorado-Fall/Poked.jpg[/COLOR]

RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\\s/+viewgallery\\.php\\?cname=([^&]+)&pcaption=([^&\\s]+) [NC]
[COLOR="#ff0000"]Using {THE_REQUEST} forces you to handle (or [B]ignore[/B]) the http and post URL data
 - I loathe using this so much I have to look it up every time I need it!
Moreover, it doesn't appear to coincide with your RewriteRule's regex. Please explain.[/COLOR]
RewriteRule ^ /photos/%1/%2.jpg? [R=302,L,NE]
[COLOR="#FF0000"]Is the {REQUEST_URI} really supposed to be empty?!?[/COLOR]

RewriteRule ^photos/([^/]+)/([^.]+)\\.jpg$ /viewgallery.php?cname=$1&pcaption=$2 [QSA,L,NC,NE] 

It just seems to me that you’re trying to avoid redirecting in a loop while using viewgallery.php to serve the .jpg image. Correct interpretation?

Regards,

DK

DK…thank you. I hope you feel better. Thank you so much for taking time to reply even when you are ill. I have written these rules with the help of a friend of mine. This is what I understand…

RewriteCond %{THE_REQUEST} [1]{3,}\s/+viewgallery\.php\?cname=([^&]+)&pcaption=([^&\s]+) [NC]
[COLOR=“#ff0000”]Using {THE_REQUEST} forces you to handle (or ignore) the http and post URL data

  • I loathe using this so much I have to look it up every time I need it!
    Moreover, it doesn’t appear to coincide with your RewriteRule’s regex. Please explain.[/COLOR]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\\s/+viewgallery\\.php\\?cname=([^&]+)&pcaption=([^&\\s]+) [NC]
RewriteRule ^ /photos/%1/%2.jpg? [R=302,L,NE]

The first one basically detects if request is made for /viewgallery.php?cname=foo&pcaption=bar while capturing foo & bar in %1 and %2.
Then it make external redirection of that request to /photos/%1/%1 i.e. /photos/foo/bar

RewriteRule ^photos/([^/]+)/([^.]+)\\.jpg$ /viewgallery.php?cname=$1&pcaption=$2 [QSA,L,NC,NE] 

This rule works matches any /photos/foo/bar request and internally forwards that to /viewgallery.php?cname=foo&pcaption=bar

So /viewgallery.php?cname=foo&pcaption=bar is always the call that is made to PHP code but a user will always send /photos/foo/bar from the browser.

It just seems to me that you’re trying to avoid redirecting in a loop while using viewgallery.php to serve the .jpg image. Correct interpretation?

I think the answer is ‘yes’ here.

RewriteBase /
This, I have found, is unnecessary and can cause unintended problems if used.

I removed this now.You did mention this before. Careless me…left this on again.

RewriteRule ^ /photos/%1/%2.jpg? [R=302,L,NE]
Is the {REQUEST_URI} really supposed to be empty?!?

I am not sure how to answer this. As I understand that is the part that converts the ugly part ‘/viewgallery.php?cname=foo&pcaption=bar’ to the format I would like to have which is ‘/photos/foo/bar’.

Here is the live demo of these rules. http://http://www.rajeevthomas.com/photos/Colorado-Fall/Touched-By-Light.jpg

I don’t know if I am wishing for too much here…but I would like the link to show up without ‘photos’ to make it show up as www.xyz.com//foo/bar or www.rajeevthomas.com/Colorado-Fall/Touched-By-Light but whenever I tried to do that , it lead to massive errors or images never showed up!.

Hope this explains everything…I am still such a newbie about all this… thank you for you help DK…

Yath.


  1. A-Z ↩︎

Hi Yath!

No worries.

It was a bit confusing (to me) the way that your code was written. Since the HTTP is not required and some other things could be better coded (IMHO, of course), let me give it a try.

[COLOR="#A9A9A9"]# RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\\s/+viewgallery\\.php\\?cname=([^&]+)&pcaption=([^&\\s]+) [NC]
#RewriteRule ^ /photos/%1/%2.jpg? [R=302,L,NE][/COLOR]

RewriteCond %{THE_REQUEST} viewgallery\\.php?cname=([^&]+)&pcaption=([^&]+)[COLOR="#0000FF"]\\.jpg[/COLOR]
# This eliminates the HTTP with version, the \\ in front of the ?,
#    the space in the pcaption's character range definition and
#    the No Case flag (you're using range definition which don't specify capitalization)
# Don't you need the .jpg stripped or is this handled by your viewgallery script?
RewriteRule .? photos/%1/%2.jpg? [R=301,L]
# I prefer an optional character to a start anchor followed by nothing but label that personal preference.
# If this is in your website's DocumentRoot, the leading / in the redirection is not required
#    (and will tell Apache to look at the [B]server's root[/B] before the website's DocumentRoot).
# 301 is a permanent redirect and forces browsers to display the .jpg link.
# I removed the NE flag ("noescape|NE 	Prevent mod_rewrite from applying [I]hexcode escaping of special characters[/I]
#    in the result of the rewrite.") because I don't understand what you're trying to do with it.

The first one basically detects if request is made for /viewgallery.php?cname=foo&pcaption=bar while capturing foo & bar.jpg in %1 and %2.
Then it make external redirection of that request to /photos/%1/%1 i.e. /photos/foo/bar (where bar ends with .jpg)

RewriteRule ^photos/([^/]+)/([^.]+)\\.jpg$ /viewgallery.php?cname=$1&pcaption=$2 [[COLOR="#FF0000"]QSA[/COLOR],L,NC,NE] 

This rule works matches any /photos/foo/bar request and internally forwards that to /viewgallery.php?cname=foo&pcaption=bar&{prior query string}

So /viewgallery.php?cname=foo&pcaption=bar is always the call that is made to PHP code but a user will always send /photos/foo/bar from the browser.

I’m a bit concerned over the .jpg and any prior query string so please label this as another source of confusion (for me).

I removed this now.You did mention this before. Careless me…left this on again.

That’s merely a “pet peeve” of mine as it generally does nothing at all … but could cause unexpected redirections.

I am not sure how to answer this. As I understand that is the part that converts the ugly part ‘/viewgallery.php?cname=foo&pcaption=bar’ to the format I would like to have which is ‘/photos/foo/bar.jpg’.

Here is the live demo of these rules. http://http://www.rajeevthomas.com/photos/Colorado-Fall/Touched-By-Light.jpg

I don’t know if I am wishing for too much here…but I would like the link to show up without ‘photos’ to make it show up as www.xyz.com//foo/bar or www.rajeevthomas.com/Colorado-Fall/Touched-By-Light but whenever I tried to do that , it lead to massive errors or images never showed up!.

Hope this explains everything…I am still such a newbie about all this… thank you for you help DK…

Actually, because this is so different than normal treatment (are your images provided as stand alone rather than embedded in web pages?) that it has led to confusion on my part … and, especially with the .jpg issue, probably on yours. Otherwise, I’d suggest kudos for how far you’ve gotten!

Regards,

DK

DK…thank you… for your help. I hope you are feeling better now.
I was trying to figure this out based on your notes.
But the rules you worked on is not redirecting the links. The original link structure shows without any changes.
And the link shows as rajeevthomas.com/viewgallery.php?cname=Colorado-Fall&pcaption=Facing-Colors ( This is the original link which is produced by the PHP code.)

So I just want make sure it is

RewriteEngine On
RewriteCond %{THE_REQUEST} viewgallery\\.php?cname=([^&]+)&pcaption=([^&]+)\\.jpg
RewriteRule .? photos/%1/%2.jpg? [R=301,L]
RewriteRule ^photos/([^/]+)/([^.]+)\\.jpg$ /viewgallery.php?cname=$1&pcaption=$2 [L,NC,NE]

But the old code (with some of the changes you suggested) is…

# Turn mod_rewrite on
RewriteEngine On
# /viewgallery.php?cname=Colorado-Fall&pcaption=Poked to /photos/Colorado-Fall/Poked.jpg
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\\s/+viewgallery\\.php\\?cname=([^&]+)&pcaption=([^&\\s]+)
RewriteRule ^ /photos/%1/%2/? [R=301,L]
RewriteRule ^photos/([^/]+)/([^.]+)/$ /viewgallery.php?cname=$1&pcaption=$2 [L,NC,NE]

And it now shows the links like this rajeevthomas.com/photos/Colorado-Fall/Touched-By-Light/

But of course the images still won’t show if they are in sub-folders. :rolleyes:

My images are not embedded.

Let me know, if you need to know more information from me.

Regards,

Yath.