Welcome to WebmasterWorld Guest from 23.23.46.20

Forum Moderators: Ocean10000 & incrediBILL & phranque

Mod rewrite and encoded URLs.

   
6:03 am on Feb 16, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



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]

4:31 pm on Feb 16, 2010 (gmt 0)

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



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
7:35 am on Feb 17, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



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]

10:45 am on Feb 17, 2010 (gmt 0)

WebmasterWorld Senior Member g1smd is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month



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.
6:13 pm on Feb 17, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



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]

11:04 pm on Feb 17, 2010 (gmt 0)

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



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
1:19 am on Feb 18, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



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]

1:33 am on Feb 18, 2010 (gmt 0)

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



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
3:07 am on Feb 18, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



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]

3:57 am on Feb 18, 2010 (gmt 0)

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



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
 

Featured Threads

Hot Threads This Week

Hot Threads This Month