Jump to content

Archived

This topic is now archived and is closed to further replies.

Mutley

mydomain.com/site as a variable?

Recommended Posts

I've done a nice bit of code for a gallery, each gallery section is divided like this:

gallery.php?id=1
gallery.php?id=2
gallery.php?id=3

etc.

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?

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
Add the following to your htaccess file:
[code]RewriteEngine On

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

If you type in site1 it'll called gallery.php with the url parameter id=1
if site99 then it'll call gallery.php with the url parameter id=99

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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=outside

Also 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.

Share this post


Link to post
Share on other sites
Can I put .htaccess in a subdirectory? If my site is there? Or does it have to be in root folder?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Thanks, another question.

So now I have this:
mysite.com/section

What if I have another subdirectory? so ?id=1&sub=3 or whatever, can I do this:
mysite.com/section/sub

Thanks alot Wild teen, very helpful. :)

Share this post


Link to post
Share on other sites
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 On

RewriteRule ^([A-Za-z]+)$ gallery.php?id=$1
RewriteRule ^([A-Za-z]+)/{[0-9+])$ gallery.php?id=$1&sub=$2 [L][/code]

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
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=$1

So 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?

Share this post


Link to post
Share on other sites
[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=$1

So 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)

Share this post


Link to post
Share on other sites
[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

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.