Forum Moderators: phranque
http://www.yoursite.com/directory/sompage.html 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 I would need to change
http://www.mysite.com/forum/profile.php?mode=viewprofile&u=76 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.
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
http://www.mysite.com/someguy http://www.mysite.com/forum/profile.php?mode=viewprofile&u=76 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 :)
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..
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.
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
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 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?
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
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]
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 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
Here is my original link:
www.example.com/item.php?id=122I'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]
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