I suck at htaccess rewrites

Soldato
Joined
27 Dec 2005
Posts
17,310
Location
Bristol
I've lost an htaccess file (deleted it because it obviously breaks Apache) and just trying to recreate a simple redirect which was when someone visits:

www.domain.com/w/d7nGhs962

...it directs to...

www.domain.com/w/?v=d7nGhs962

All the examples I can find are the opposite (dirty to clean) and I can't seem to get anything to work by switching them around.

Basically I want the variable - in this case a YouTube ID - to be passed as a variable rather than the browser thinking it's a folder. So far I've got:

RewriteEngine ON
RewriteRule ^/w/(.+) /w/?v=$1 [L]

TIA!
 
Last edited:
Most of my stuff is hosted on IIS so I'm not very good with .htaccess files. I'll leave that to others to answer.

just trying to recreate a simple redirect
The problem you have is that it looks like a simple redirect at first but it's actually a lot more complicated than doing it the other way around.
The URL is interpreted in 2 parts, the base address and a query string. In your example (www.domain.com/w/?v=d7nGhs962), the base address is www.domain.com/w/ and the query string is v=d7nGhs962. It's quite easy to take these 2 parts and join them together but it's more complicated to try and do it the other way around.
What you have is www.domain.com/w/d7nGhs962 as the base address and a blank query string (ie. You dont have any variables to reference).

Do you not have any control over how the users access that address? It would be easier to fix it at source (the links people click on) than by redirecting.
 
Either that, or write a script, in php or similar that reads the URL, processes it and redirects. Use that as your index page in domain/w/ and use htaccess to redirect everything to domain/w/

I've done that before to fix similar problems and it works fine. It's a way of avoiding the regex.

Either that or try something like

RewriteRule ^/([^/])/w/? ?v=$1
 
Back
Top Bottom