Forum Moderators: phranque

Message Too Old, No Replies

RewriteRule Help

RewriteRule Help

         

dk20005

6:19 am on Aug 4, 2012 (gmt 0)

10+ Year Member



would like to rewrite this:

http://www.example.com/livestream?liveview=THISISTHEVARIABLE&mode=normal&type=twitch

to this:

http://www.example.com/?THISISTHEVARIABLE

is this possible I can't seem to get it to work.

Thanks for your help in advance.

lucy24

8:21 am on Aug 4, 2012 (gmt 0)

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



You need to explain what you mean by "rewrite A to B" because it's possible you have got it backward. Also sideways, because you probably need both a Redirect and a Rewrite.

What does the user start out requesting? Are we talking about type-ins or clicked links?

What do you want the user to see?

What is the actual content, and where does it live?

What's all that extra stuff in the query string? Where do you want it to go?

And, finally: What does your best effort look like so far?

Scroll back a few threads in this forum and you'll find the boilerplate about "Why we ask you to do it yourself". I distinctly remember posting it not more than 12 hours ago.

g1smd

7:40 pm on Aug 4, 2012 (gmt 0)

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



A redirect is a URL to URL translation.

A rewrite is a URL to filepath translation.

You're going to need one of each but from the original question it isn't clear which way round these rules need to act.

dk20005

7:51 pm on Aug 4, 2012 (gmt 0)

10+ Year Member



Sorry a newbie to this site and apache rewrite.

What i'm trying to do is make it easy for users to link to their video streams which reside at http://www.example.com/livestream?liveview=THISISTHEIRUSERNAME&mode=normal&type=twitch

I would like them to type in the url www.example.com/?THEREUSERNAME to access the above page.

I tried to read up and figure it out but was not quite understanding how it works.

lucy24

8:42 pm on Aug 4, 2012 (gmt 0)

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



OK, now do a second Forums search for "query string" + boilerplate.* The part you need to read slowly is the part about capturing and reusing part of a query string. If you stumble across the third piece of boilerplate (I've got three) which is about rewriting-and-redirecting, you can read that too ;)

Is it really "?USERNAME" like that, with question mark but without parameter name ("name=USERNAME")?

As long as you're rewriting anyway, why bother with the question mark at all? Just let them request www.example.com/username or similar and then rewrite to http://www.example.com/livestream?liveview=username&mode=normal&type=twitch

Make sure you have code that constrains the username to a form that's easy for you to work with. Ideally nothing but alphanumerics. Maybe hyphens or lowlines; definitely no spaces.

Is it just one link per username, so the request will always be in the same form?


* Yes, I could easily paste it in anew every time the question arises. But then the Forums themselves would start racking up Duplicate Content and the People Up Top would be giving me dirty looks.

dk20005

10:27 pm on Aug 4, 2012 (gmt 0)

10+ Year Member



Appreciate the point in the right direction now off to learning how to implement it. I wasn't expecting to not have to learn :)

dk20005

10:30 pm on Aug 4, 2012 (gmt 0)

10+ Year Member



Oh yes it is a script that runs and embeds their twitch tv stream into a page on our site. so it will always be the same just a different username with the same string.

dk20005

5:44 pm on Aug 10, 2012 (gmt 0)

10+ Year Member



Ok I am still having trouble with this code below

RewriteEngine On
RewriteCond %{REQUEST_URI} ^/$
RewriteCond %{QUERY_STRING} ^?liveview=([0-9,a-Z]*)$&mode=normal&type=twitch
RewriteRule ^(.*)$ http://www.example.com/livestream [L,R=301]


trying to go from type in of example2.com/NAMEHERE(this is variable)

to(yes a different domain eg example vs example2) www.example.com/livestream?liveview=NAMEHERE&mode=normal&type=twitch

can anyone help me figure out what i am doing wrong?

g1smd

6:31 pm on Aug 10, 2012 (gmt 0)

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



^/$ - matches only a request for root.

[0-9,a-Z] - do your URLs contain commas?

$& - looks like a typo.

([0-9a-Z]*) - the * allows the value to be missing.

dk20005

7:13 pm on Aug 10, 2012 (gmt 0)

10+ Year Member



ok I removed the * and removed the comma. but not quite understanding what i need to do with the first part with the $ tag and the &mode is part of the query string but from my understand the $ is where the value of the first line is inserted.

dk20005

11:43 am on Aug 12, 2012 (gmt 0)

10+ Year Member



With the help of a friend we got to here where the redirect does work but it redirects everything to the site with the query string. I still want the site to display the index page of the servers main page if it is example.com followed by nothing I don't want it to redirect. How can I make this happen?

RewriteEngine On
RewriteCond %{DOCUMENT_ROOT}/ !-f
RewriteRule ^/?(.*)$ http://www.example.com/livestream?liveview=$1&mode=normal&type=twitch [R]

g1smd

2:06 pm on Aug 12, 2012 (gmt 0)

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



With protocol, hostname and [R] flag you have a redirect, not a rewrite.

lucy24

10:22 pm on Aug 12, 2012 (gmt 0)

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



I still want the site to display the index page of the servers main page if it is example.com followed by nothing

You do this by making a separate rule whose Pattern is empty. Or / alone if this is happening in the config file. With optional (index\.html)? -- or whatever your index file extension is -- because those need to be redirected at the same time. (Requests ending in "index.html" would normally be your second-to-the-last generic redirect, before the final with/without www if you aren't letting your host do it.)

:: memo to self: ask son to edit those /games/index.html links on his site so I don't have to redirect them ::

g1smd

12:24 am on Aug 13, 2012 (gmt 0)

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



I still want the site to display the index page of the servers main page if it is example.com followed by nothing

DirectoryIndex index.html



edit those /games/index.html links on his site so I don't have to redirect them

Oh, even after editing them, you still need thse redirects (or at the very least the
rel="canonical"
tag).

lucy24

6:46 am on Aug 13, 2012 (gmt 0)

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



DirectoryIndex index.html

I thought he meant that he didn't want the Redirect to happen if the request is for the front page.