homepage Welcome to WebmasterWorld Guest from 54.145.183.169
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Home / Forums Index / Code, Content, and Presentation / Apache Web Server
Forum Library, Charter, Moderators: Ocean10000 & incrediBILL & phranque

Apache Web Server Forum

    
Mod rewrite and encoded URLs.
Jesse_Smith

WebmasterWorld Senior Member 10+ Year Member



 
Msg#: 4081041 posted 6:03 am on Feb 16, 2010 (gmt 0)

Is it possible to do mod_rewrite with an encoded URL in the URL?

Original URL...

http://www.example.com/index.php?url=http%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3DZZZZZZZ

mod_rewrite URL....

http://www.example.com/v/ZZZZZZZ/

RewriteRule ^v/([^.]+)/$ index.php?url=http%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3D$1 [L]

[edited by: jdMorgan at 4:01 am (utc) on Feb 18, 2010]
[edit reason] example.com [/edit]

 

jdMorgan

WebmasterWorld Senior Member jdmorgan us a WebmasterWorld Top Contributor of All Time 10+ Year Member



 
Msg#: 4081041 posted 4:31 pm on Feb 16, 2010 (gmt 0)

RewriteRule cannot 'see' query strings.

Use a RewriteCond testing %{QUERY_STRING} to see the URL-decoded query string.

Use a RewriteCond testing %{THE_REQUEST} to see the un-decoded request line, exactly as received from the client. This will include the HTTP Method, the URL-path, the query string, any URL-fragments/named anchors, and the protocol (HTTP/1.0 or HTTP1.1) -- exactly as the request appears in your raw server access log file.

But before looking into this level of detail, be aware that it appears that your rule is exactly backwards from your stated requirements. Looking at your code, it appears that you wish to redirect example.com/v/ZZZ to youtube with "ZZZ" as the query string. If this is the case, then your rule should work, except that I see no "set-up" directives such as "Options" or "RewriteEngine on" in your posted example, and the rule pattern should probably be changed to "^v/([^/]+)/$" for efficiency and precision.

If your intent is to change the URLs shown on your pages, then edit those HTML pages or the script that produces those pages; mod_rewrite cannot change the links on your pages, and those links 'define' the URLs. Once 'defined' nothing can change those URL except the pages where they are defined.

Jim

Jesse_Smith

WebmasterWorld Senior Member 10+ Year Member



 
Msg#: 4081041 posted 7:35 am on Feb 17, 2010 (gmt 0)

Is it possible to redirect it? You submit a form and then up at the long encoded

http://www.example.com/index.php?url=http%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3DZZZZZZZ

URL that doesn't work, with

http://www.example.com/v/ZZZZZZZ/

being where they should be at, that works.


Options +Indexes
Options +FollowSymlinks
RewriteEngine on
RewriteBase /
RewriteRule ^v/([^.]+)/$ index.php?v=$1 [L]
RewriteRule ^s/([^.]+)/$ index.php?v=$1 [L]
RewriteRule ^index.php?v=http://www.youtube.com/watch?v=([^/]+)$ http://www.example.com/v/$1/ [R=301,L]
RewriteRule ^index.php?v=http%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3D([^/]+)$ http://www.example.com/v/$1/ [R=301,L]

Both encoded and non-encoded doesn't do it.

[edited by: jdMorgan at 4:00 am (utc) on Feb 18, 2010]
[edit reason] example.com [/edit]

g1smd

WebmasterWorld Senior Member g1smd us a WebmasterWorld Top Contributor of All Time 10+ Year Member



 
Msg#: 4081041 posted 10:45 am on Feb 17, 2010 (gmt 0)

RewriteRule cannot 'see' query strings.

Use a RewriteCond testing %{QUERY_STRING} to see the URL-decoded query string.

Place your rewrites AFTER the redirects to avoid multiple problems.

Jesse_Smith

WebmasterWorld Senior Member 10+ Year Member



 
Msg#: 4081041 posted 6:13 pm on Feb 17, 2010 (gmt 0)

RewriteCond testing %{QUERY_STRING}

just spits out an Internal Server Error.

RewriteCond %{QUERY_STRING} ^v=http%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3D([^/]+)?$
RewriteRule ^index\.php$ http://www.example.com/v/([^/]+)/ [R=301,L]

and you get redirected a tiny bit, redirects to

domain.com/v/(%5b%5e&%5d+)/?v=http%253A%252F%252Fwww.youtube.com%252Fwatch%253Fv%253D9lp0IWv8QZY

The non-encoded version...

RewriteCond %{QUERY_STRING} ^v=http://www.youtube.com/watch?v=([^&]+)?$
RewriteRule ^index\.php$ http://www.example.com/v/([^&]+)/ [R=301,L]

does nothing.

[edited by: jdMorgan at 3:59 am (utc) on Feb 18, 2010]
[edit reason] example.com [/edit]

jdMorgan

WebmasterWorld Senior Member jdmorgan us a WebmasterWorld Top Contributor of All Time 10+ Year Member



 
Msg#: 4081041 posted 11:04 pm on Feb 17, 2010 (gmt 0)

The substitution in your rule has a regular-expressions pattern in it. Your RewriteCond has a a whole bunch of un-escaped mod_rewrite-reserved characters in it. I'd expect to get several kinds of errors with that.

How about trying something like:

RewriteCond %{THE_REQUEST} ^[A-Z]+\ /index\.php\?v=http\%3A\%2F\%2Fwww\.youtube\.com\%2Fwatch\%3Fv\%3D([^\ ]+)\ HTTP/
RewriteRule ^index\.php$ http://www.example.com/v/%1/ [R=301,L]

This is going to take an incoming browser request for
example.com/index.php?v=http%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3DxYzAbC
and redirect the client to
example.com/v/xYzAbC/

Jim

Jesse_Smith

WebmasterWorld Senior Member 10+ Year Member



 
Msg#: 4081041 posted 1:19 am on Feb 18, 2010 (gmt 0)

Now the redirect does make it show up with /v/ZZZZZZZ/

Would it be the script that's making the extra part still show up as

http://www.example.com/v/9lp0IWv8QZY/?v=
http%253A%252F%252Fwww.youtube.com%252Fwatch%253Fv%253D9lp0IWv8QZY

The form uses GET.

[edited by: jdMorgan at 3:58 am (utc) on Feb 18, 2010]
[edit reason] example.com [/edit]

jdMorgan

WebmasterWorld Senior Member jdmorgan us a WebmasterWorld Top Contributor of All Time 10+ Year Member



 
Msg#: 4081041 posted 1:33 am on Feb 18, 2010 (gmt 0)

No. Put a question mark at the end of the "new" URL in the RewriteRule. That will clear the query string.

Second time today I forgot to do that... :(

Jim

Jesse_Smith

WebmasterWorld Senior Member 10+ Year Member



 
Msg#: 4081041 posted 3:07 am on Feb 18, 2010 (gmt 0)

Thanks! Now it works. This is even better. I edited your code to make domain.com/index.php?v=ZZZZZZ also get redirected, when just the video ID is entered in the form.

RewriteCond %{THE_REQUEST} ^[A-Z]+\ /index\.php\?v=http\%3A\%2F\%2Fwww\.youtube\.com\%2Fwatch\%3Fv\%3D([^\ ]+)\ HTTP/
RewriteRule ^index\.php$ http://www.example.com/v/%1/? [R=301,L]

RewriteCond %{THE_REQUEST} ^[A-Z]+\ /index\.php\?v=([^\ ]+)\ HTTP/
RewriteRule ^index\.php$ http://www.example.com/v/%1/? [R=301,L]

RewriteRule ^v/([^.]+)/$ index.php?v=$1 [L]

[edited by: jdMorgan at 3:58 am (utc) on Feb 18, 2010]
[edit reason] example.com [/edit]

jdMorgan

WebmasterWorld Senior Member jdmorgan us a WebmasterWorld Top Contributor of All Time 10+ Year Member



 
Msg#: 4081041 posted 3:57 am on Feb 18, 2010 (gmt 0)

Combine two into one for additional savings:


RewriteCond %{THE_REQUEST} ^[A-Z]+\ /index\.php\?v=[b]((h[/b]ttp\%3A\%2F\%2Fwww\.youtube\.com\%2Fwatch\%3Fv\%3[b]D)?[/b][^\ ]+)\ HTTP/
RewriteRule ^index\.php$ http://www.example.com/v/%1/? [R=301,L]

Sure would appreciate it if you'd use "example.com"...

Jim

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / Apache Web Server
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved