Page 1 of 1

mod_rewrite links

Posted: Sat Dec 18, 2004 6:42 am
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.

Posted: Sat Dec 18, 2004 10:03 am
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.

Posted: Sat Dec 18, 2004 3:39 pm
by kc0hs
No luck... Doesn't seem to work.

Posted: Sat Dec 18, 2004 3:53 pm
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.

Posted: Sat Dec 18, 2004 4:06 pm
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?

Posted: Sat Dec 18, 2004 10:28 pm
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

Posted: Sat Jan 22, 2005 3:07 pm
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

Posted: Sat Jan 22, 2005 3:32 pm
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?

Posted: Sat Jan 22, 2005 7:31 pm
by feyd
did you place this htaccess file into that subdomain's directory?

Posted: Sat Jan 22, 2005 8:08 pm
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?

Posted: Sat Jan 22, 2005 8:12 pm
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

Posted: Sat Jan 22, 2005 8:17 pm
by The Monkey
Ah, I wasn't aware of that. Thank you, I will place the htaccess files in my subdomains, as well.