Mutley Posted September 29, 2006 Share Posted September 29, 2006 I've done a nice bit of code for a gallery, each gallery section is divided like this:gallery.php?id=1gallery.php?id=2gallery.php?id=3etc.Can I make it so if you put in www.mydomain.com/site1... it goes to gallery.php?id=2 and again for .com/site2 goes to gallery.php?id=2 etc.Is this possible, if so how? Quote Link to comment Share on other sites More sharing options...
steveclondon Posted September 29, 2006 Share Posted September 29, 2006 yes it is. If you want to do it exactly as you have there you would do it by using mod_rewite. First you have to make sure that you have access to the .htaccess file on your server Quote Link to comment Share on other sites More sharing options...
Mutley Posted September 29, 2006 Author Share Posted September 29, 2006 Any examples? I have htaccess, not sure how to use it though. Quote Link to comment Share on other sites More sharing options...
wildteen88 Posted September 29, 2006 Share Posted September 29, 2006 Add the following to your htaccess file:[code]RewriteEngine OnRewriteRule ^site([0-9]+)$ gallery.php?id=$1[/code]If you type in site1 it'll called gallery.php with the url parameter id=1if site99 then it'll call gallery.php with the url parameter id=99 Quote Link to comment Share on other sites More sharing options...
Mutley Posted September 29, 2006 Author Share Posted September 29, 2006 Thanks, so if I wanted sections called "outside" and "hobbies" instead of "site1" and "site 2", what would I put?Is there a way so when you add new sections you don't have to keep editing the htaccess? Quote Link to comment Share on other sites More sharing options...
wildteen88 Posted September 29, 2006 Share Posted September 29, 2006 Rather than send a numerical id to gallery.php you'll want to send a string instead. So you might want to do something like this, if you want to use outside, hobbies rather than site1, site2 or sitex (x being a number):[code]RewriteRule ^([A-Za-z]+)$ gallery.php?page=$1[/code]This time when you do something like this:mysite.com/outside it'll call gallery.php with the url parameter id=outsideAlso with that you dont have to keep editing the htaccess file. However you may need to chnage the code in gallery.php though now that you are sending a string rather than a number in the id variable. Quote Link to comment Share on other sites More sharing options...
Mutley Posted September 29, 2006 Author Share Posted September 29, 2006 Can I put .htaccess in a subdirectory? If my site is there? Or does it have to be in root folder? Quote Link to comment Share on other sites More sharing options...
wildteen88 Posted September 29, 2006 Share Posted September 29, 2006 htaccess folder can be placed anywhere. So you can place it in the subdirecotry if you want. However keep in mind what ever setting/configuration you have in the htaccess file will work when you go to the subfolder you put the htaccess file in. Quote Link to comment Share on other sites More sharing options...
Mutley Posted September 29, 2006 Author Share Posted September 29, 2006 Thanks, another question.So now I have this:mysite.com/sectionWhat if I have another subdirectory? so ?id=1&sub=3 or whatever, can I do this:mysite.com/section/subThanks alot Wild teen, very helpful. :) Quote Link to comment Share on other sites More sharing options...
wildteen88 Posted September 30, 2006 Share Posted September 30, 2006 Yeah. You just need to add a newr rewrite rule after your other one for that:[code]RewriteRule ^([A-Za-z]+)/{[0-9+])$ gallery.php?id=$1&sub=$2[/code]So your htaccess file should like this:[code]RewriteEngine OnRewriteRule ^([A-Za-z]+)$ gallery.php?id=$1RewriteRule ^([A-Za-z]+)/{[0-9+])$ gallery.php?id=$1&sub=$2 [L][/code] Quote Link to comment Share on other sites More sharing options...
Mutley Posted September 30, 2006 Author Share Posted September 30, 2006 It's not seeming to work the last part.If I do:mysite.com/test...it works fine, however if I do:mysite.com/test/1...it does a 404 Not found? Quote Link to comment Share on other sites More sharing options...
oldmanice Posted October 1, 2006 Share Posted October 1, 2006 Try mysite.com/test1 Quote Link to comment Share on other sites More sharing options...
Mutley Posted October 1, 2006 Author Share Posted October 1, 2006 Nope, that doesn't work, it just thinks it's called "nick1". Quote Link to comment Share on other sites More sharing options...
wildteen88 Posted October 1, 2006 Share Posted October 1, 2006 I did a typo, I typed { instead of (. So use this as the last rewrite rule:[code]RewriteRule ^([A-Za-z]+)/([0-9+])$ gallery.php?id=$1&sub=$2 [L][/code] Quote Link to comment Share on other sites More sharing options...
shawn2page Posted October 20, 2006 Share Posted October 20, 2006 This code has been helpful for me but I am having a problem. I have set the code up as follows:RewriteRule ^([A-Za-z]+)$ profile.php?ID=$1So is they type www.mysite.com/johndoe it will take them to johndoe's page on our site. But the problem I have is if a member signs up with member name mark2000 the code will not work. How can i set it up to work with numbers? Quote Link to comment Share on other sites More sharing options...
FuzzyLogik Posted December 12, 2006 Share Posted December 12, 2006 [quote author=shawn2page link=topic=109959.msg454799#msg454799 date=1161326102]This code has been helpful for me but I am having a problem. I have set the code up as follows:RewriteRule ^([A-Za-z]+)$ profile.php?ID=$1So is they type www.mysite.com/johndoe it will take them to johndoe's page on our site. But the problem I have is if a member signs up with member name mark2000 the code will not work. How can i set it up to work with numbers?[/quote]Seconded. How would I change the expression to allow number as well (in any order) Quote Link to comment Share on other sites More sharing options...
Mutley Posted December 22, 2006 Author Share Posted December 22, 2006 [quote author=wildteen88 link=topic=109959.msg444681#msg444681 date=1159710312]I did a typo, I typed { instead of (. So use this as the last rewrite rule:[code]RewriteRule ^([A-Za-z]+)/([0-9+])$ gallery.php?id=$1&sub=$2 [L][/code][/quote]How would you do it with underscores? Such as:mysite.com/this_is_my_page Quote Link to comment Share on other sites More sharing options...
Mad.Man Posted December 22, 2006 Share Posted December 22, 2006 RewriteRule ^([A-Za-z]+)/([0-9+])$ gallery.php?id=$1&sub=$2 [L] this_is_my_page Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.