mod_rewrite links

Need help installing PHP, configuring a script, or configuring a server? Then come on in and post your questions! We'll try to help the best we can!

Moderator: General Moderators

Post Reply
kc0hs
Forum Newbie
Posts: 4
Joined: Fri Dec 17, 2004 5:59 pm

mod_rewrite links

Post by kc0hs »

Hello.

I was wondering how I can shorted links in my website using mod_rewrite, such an example is at http://www.def-con.org

I want to be able to to go links with domain.com/l/link.com and basically a replica of the system available on the website mentioned above.

And also a similar for pages as is on that website.

Please give help and advice on how to do this as I have not yet found a way.

Thanks and regards.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

Code: Select all

RewriteEngine On 
RewriteBase / 

RewriteRule ^s/(їA-Za-z]+)$ /?page=$1 їL]
I checked out def-con.org what it looks like thats what they do.
kc0hs
Forum Newbie
Posts: 4
Joined: Fri Dec 17, 2004 5:59 pm

Post by kc0hs »

No luck... Doesn't seem to work.
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post by markl999 »

RewriteEngine On
RewriteRule ^l/(.+) http://$1 [L]

A very basic example. You'll need to tweak the regex (.+) to be a bit more strict.
kc0hs
Forum Newbie
Posts: 4
Joined: Fri Dec 17, 2004 5:59 pm

Post by kc0hs »

Thanks for this help.

Please could it be elaborated as to what is happening in Phenom's code?

It doesn't seem to work to successfully with me?
rehfeld
Forum Regular
Posts: 741
Joined: Mon Oct 18, 2004 8:14 pm

Post by rehfeld »

it helps when you provide info about your problems.

for example, what exactly leads you to beleive its "not working" for you?
error message? if so, whats the error message?

phenoms code could be read as follows:
match a url which begins with s/
and the rest of the url is ONLY letters (case insensitive a-z)
and that rest of the url part must be at least 1 character long
if so, capture everything after s/ and rewrite it to the document root,
and set the query string to ?$page=captured_url_here

this would most likely rewrite to your index.php file
then inside that php file, you would access that data via $_GET['page']

foo.com/s/something --- this would be rewritten
foo.com/s/something/ --- this wouldnt, because / is not a letter

you could make the trailing / optional, and then both examples would work

there isnt a one size fits all to rewriting urls.

this is untested, but should rewrite any url, that is NOT a directory, and NOT an existing file, to index.php

then in php, you can access the url they requested by using one of the $_SERVER variables, and do your parsing there.

Code: Select all

RewriteEngine On
RewrtiteCond %{REQUEST_FILENAME} !-d # not a dir
RewrtiteCond %{REQUEST_FILENAME} !-f # not a file
RewriteRule .* index.php їR]
this in a sense, eliminates error-404's

try that and see if you like it. use it for a bit, soon youll have a better idea of exactly what it is you need.

there countless resources on mod_rewrite via google. understanding regex is imo 90% of understanding mod rewrite, so that may be what you should learn the basics of first.
you should be able to learn some solid regex basics in an hour or 2
The Monkey
Forum Contributor
Posts: 168
Joined: Tue Mar 09, 2004 9:05 am
Location: Arkansas, USA

Post by The Monkey »

I'm sorry to bump, but I wanted to point out a problem with rehfeld's code; it had me stumped until I looked in my server's error logs. It's a very cleverly hidden typo. :P

It should read:

Code: Select all

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d # not a dir
RewriteCond %{REQUEST_FILENAME} !-f # not a file
RewriteRule .* index.php їR]
He had two of the lines written as "rewrtite".

That said, thank you for the code, rehfield! This is exactly what I was looking for, and it works fantastic once your code's typo is corrected. :p
rehfeld wrote: this is untested, but should rewrite any url, that is NOT a directory, and NOT an existing file, to index.php

then in php, you can access the url they requested by using one of the $_SERVER variables, and do your parsing there.

Code: Select all

RewriteEngine On
RewrtiteCond %{REQUEST_FILENAME} !-d # not a dir
RewrtiteCond %{REQUEST_FILENAME} !-f # not a file
RewriteRule .* index.php їR]
this in a sense, eliminates error-404's

try that and see if you like it. use it for a bit, soon youll have a better idea of exactly what it is you need.

there countless resources on mod_rewrite via google. understanding regex is imo 90% of understanding mod rewrite, so that may be what you should learn the basics of first.
you should be able to learn some solid regex basics in an hour or 2
The Monkey
Forum Contributor
Posts: 168
Joined: Tue Mar 09, 2004 9:05 am
Location: Arkansas, USA

Post by The Monkey »

Interestingly enough, the code that I thought worked perfectly needs a bit more tweaking:

Code: Select all

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d # not a dir
RewriteCond %{REQUEST_FILENAME} !-f # not a file
RewriteRule .* index.php їR]
If I attempt to access a non-existing file in a subdomain, such as "http://subdomain.mysite.com/nonexistantfile", it doesn't work.

Is there a workaround, if so, what is it, or am I just stuck?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

did you place this htaccess file into that subdomain's directory?
The Monkey
Forum Contributor
Posts: 168
Joined: Tue Mar 09, 2004 9:05 am
Location: Arkansas, USA

Post by The Monkey »

feyd wrote:did you place this htaccess file into that subdomain's directory?
No, I didn't, I placed it in the public_html directory. The subdomain's directory is /public_html/subdomain/. Shouldn't the subdomain inherit it's parent's htaccess settings?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

only when accessed via the directory tree, I believe.. for example...

http://www.yoursite.com/subdomain/this_aint_here.html

vs

subdomain.yoursite.com/this_aint_here.html
The Monkey
Forum Contributor
Posts: 168
Joined: Tue Mar 09, 2004 9:05 am
Location: Arkansas, USA

Post by The Monkey »

Ah, I wasn't aware of that. Thank you, I will place the htaccess files in my subdomains, as well.
Post Reply