mod_rewrite links
Moderator: General Moderators
mod_rewrite links
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.
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.
- John Cartwright
- Site Admin
- Posts: 11470
- Joined: Tue Dec 23, 2003 2:10 am
- Location: Toronto
- Contact:
Code: Select all
RewriteEngine On
RewriteBase /
RewriteRule ^s/(їA-Za-z]+)$ /?page=$1 їL]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.
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
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]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
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. 
It should read:
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
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]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.
this in a sense, eliminates error-404'sCode: Select all
RewriteEngine On RewrtiteCond %{REQUEST_FILENAME} !-d # not a dir RewrtiteCond %{REQUEST_FILENAME} !-f # not a file RewriteRule .* index.php їR]
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
Interestingly enough, the code that I thought worked perfectly needs a bit more tweaking:
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?
Code: Select all
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d # not a dir
RewriteCond %{REQUEST_FILENAME} !-f # not a file
RewriteRule .* index.php їR]Is there a workaround, if so, what is it, or am I just stuck?
-
The Monkey
- Forum Contributor
- Posts: 168
- Joined: Tue Mar 09, 2004 9:05 am
- Location: Arkansas, USA
- feyd
- Neighborhood Spidermoddy
- Posts: 31559
- Joined: Mon Mar 29, 2004 3:24 pm
- Location: Bothell, Washington, USA
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
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