Forum Moderators: phranque

Message Too Old, No Replies

Short URL's with .htaccess

Not sure if this is possible..

         

cynikalsam

10:35 pm on Dec 28, 2004 (gmt 0)

10+ Year Member



I heard that .htaccess can help in making short URL's. like changing
http://www.yoursite.com/directory/sompage.html

to
http://www.yoursite.com/somepage

I need to know if this can work dynamically. I'll tell you exactly what i want to do, and if you know, just tell me if its possible or not (and if you know how to actually do it, that would be helpful too)

There are 3 things i would like to make into short URL's. But i'll just use this one as an example.

The user profile page is located

http://www.mysite.com/forum/profile.php?mode=viewprofile&u=X

Where "X" is the user I.D number from my forum. Lets say the username is "someguy" and someguy has a user I.D of 76. (this information is stored in an sql database).

I would need to change

http://www.mysite.com/forum/profile.php?mode=viewprofile&u=76

To
http://www.mysite.com/someguy

And i wont be able to do this manually for each user, so the code would have to do this automatically. So by checking the username in the database, you would be redirected to the correct long URL with the user I.D number at the end.

I hope i explained this well. If anybody has any idea if this is possible, or how to do this, i'd really appreciate any help.

jdMorgan

1:41 am on Dec 29, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



cynikalsam,

Yes, it's possible, but it's important that you understand how it works before starting. Many people get the concept somewhat backwards, and waste a lot of time because of it.

First, change the links on your pages so that they are in the short form. This can easly be done with php's preg_replace function just after you 'build' the link from your database info. Sometimes, preg_replace isn't even needed; the link can be built directly from the database info.

Now that your page script outputs short URLs, visitors and search engine crawlers will "see" them and then request those short URLs from your server. When the request arrives at your server, use mod_rewrite to convert the requested short URL back to the longer-URL-with-query-string form needed to call your script. Now the circle is complete, with short URLs visible on the Web, but no change in how you call your script.

Try a search here for "mod_rewrite static dynamic URL" for lots of examples.

Jim

cynikalsam

3:20 am on Dec 29, 2004 (gmt 0)

10+ Year Member



So, if i enter the URL
http://www.mysite.com/someguy

and i want it to get
http://www.mysite.com/forum/profile.php?mode=viewprofile&u=76 

I would put the .htaccess file in the public_html root of my server? (because there really is no "/someguy/" directory..

Im asking because i would have no idea how to get the .htaccess file to get that information from the database.

I tried searching this site, and i saw plenty of results, but if i try to click on a result to read the post, im asked to log in again (even though i was already logged in) then im dropped off at the front page.. I cant seem to get into my search results.

Any additional help as far as what code i would need to put in the .htaccess file to get information from the database, then make short url's out of it would be helpful. In the meantime i'll try searching those terms on google.

Thanks :)

jdMorgan

3:39 am on Dec 29, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



So, if i enter the URL
[mysite.com...]
and i want it to get
[mysite.com...]
I would put the .htaccess file in the public_html root of my server? (because there really is no "/someguy/" directory..

Yes.

Im asking because i would have no idea how to get the .htaccess file to get that information from the database.

.htaccess doesn't need to access the database, it simply moves the "pieces" of the requested URL around, and can fill in constant/fixed/unchanging information. Your script outputs a short URL (using the database as it does now, but formatting the link in a shorter way), the client requests the short URL, .htaccess intercepts the request, moves the forum name from the URL into the query string, adds a few more characters, and invokes your script with the same kind of URLs you used to use. It's very simple, once you're used to it.

I tried searching this site, and i saw plenty of results, but if i try to click on a result to read the post, im asked to log in again (even though i was already logged in) then im dropped off at the front page.. I cant seem to get into my search results.

This site requires cookies. If your browser blocks them or if you run Norton Internet Security with the security settings too high, then cookies won't work, and the site won't be able to track what page you are on or where you want to go. (This site uses "short" URLs on the Web side, and dynamic URLs internally too, BTW.) Look at your system settings in the WebmasterWorld "control panel" (link at top left of this page) for help with WebmasterWorld cookies.

Any additional help as far as what code i would need to put in the .htaccess file to get information from the database, then make short url's out of it would be helpful. In the meantime i'll try searching those terms on google.

We can help you learn to write the code, but we can't write your code -- the demand would simply be too high to keep up with that kind of request. See our forum charter for helpful references, and consider installing a Google site search [webmasterworld.com] function in your WebmasterWorld custom settings.

Jim

cynikalsam

7:30 am on Dec 29, 2004 (gmt 0)

10+ Year Member



Yeah if you guys can help me learn how to code this, that would be great. I fixed my firewall settings and read a bunch of helpful articles, but there is something im not quite sure of.

In every example i found, the .htaccess takes characters and words from the origional URL and changes them.

like changing

http://www.mysite.com/directory/category.php?id=2

in to
http://www.mysite.com/directory/category/id/2

And all the elements of that url are defined in the .htaccess file in order to rewrite. (category, id, 2 etc..)

So, using my example, would i have to manually put the username "someguy" somewhere in the .htaccess file? If not, how will the .htaccess file know to what to do with it?

jdMorgan

5:21 pm on Dec 29, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



> In every example i found, the .htaccess takes characters and words from the origional URL and changes them.

Yes, by definition, that's how it works.

If you wish to do some sort of "lookup" then you'll need to use a much more complex solution, not available in .htaccess. If you have access to httpd.conf, you can use mod_rewrite's RewriteMap directive to lookup up the id number using the "someguy" variable, but this directive is not available in an .htaccess context. It's also not terribly efficient.

The easiest approach is to simply use the id number in the short URL, and let the php script do all the database lookup functions to relate that number to "someguy".

mod_rewrite is basically a URL-text-substitution mechanism, and not much more.

Jim

iclaudius

5:08 pm on Jan 12, 2005 (gmt 0)

10+ Year Member



Doing my best to use .htaccess but I can't get my links to clean up. Any ideas?

Here is my original link:
www.example.com/item.php?id=122

I'd like it to look more like
www.example.com/products/122/
or any variation on this

So I changed my rewrite file as such

RewriteEngine On
RewriteRule ^/([0-9]+)/$ item.php?id=$1

- but it doesn't seem to work! Any tips hugely appreciated. Thanks
Peter
Vermont

[edited by: jdMorgan at 5:34 pm (utc) on Jan. 12, 2005]
[edit reason] Removed specifics. Please see TOS. [/edit]

jdMorgan

5:43 pm on Jan 12, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



iclaudius,

Welcome to WebmasterWorld!

Your regular expressions pattern "^/([0-9]+)/$" does not match "products/122/", so the rule will always fail. Take a look at the regular expressions tutorial cited in our charter [webmasterworld.com] for some help.

Also, be aware that in an .htaccess context, the local URL-path "seen" by RewriteRule is stripped of its leading slash, so your pattern should not start with a slash.

It is also possible that you will need to add

 Options +FollowSymLinks 

at the beginning of your code. This is server config dependent. If rewrites are failing because this is missing, then this will be indicated in your server error log file.

Jim

iclaudius

12:37 pm on Jan 13, 2005 (gmt 0)

10+ Year Member



Dear Jim

Thanks a lot for your help with this. I followed you tips and rewrote my code as:

Options +FollowSymLinks

RewriteEngine On

RewriteRule ^item/([0-9]+)/$ item.php?id=$1

--
removed the slash and inserted the followsymlinks at the start, but it still does not seem to have any affect!

I've spent some time poring over those throroughly written tutorials but I start to get lost about 2 sentences in...

Any more suggestions would be appreciated.
With thanks,
Peter

jdMorgan

4:08 pm on Jan 13, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Here is my original link:
www.example.com/item.php?id=122

I'd like it to look more like
www.example.com/products/122/
or any variation on this


 Options +FollowSymLinks
RewriteEngine On
RewriteRule ^[b]products[/b]/([0-9]+)/$ /item.php?id=$1 [L]

Assuming that mod_rewrite is loaded on your server, if you request "example.com/products/<number>/", the code will rewrite that to "example.com/item.php?id=<number>".

You will then need to change the php code so that it produces the short URLs for the products on all of the pages that it generates.

Jim

iclaudius

4:41 pm on Jan 13, 2005 (gmt 0)

10+ Year Member



Fantastic Jim!

Thanks for all your help and persistence with this.

Cheers - Peter