The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> System Administration
> Apache Development
|
Rewrite to different kinds of php conditions
Discuss Rewrite to different kinds of php conditions in the Apache Development forum on Dev Shed. Rewrite to different kinds of php conditions Apache Development forum discussing HTTP Server general topics, configuration, and modules. Apache is an open source web server that runs on multiple platforms.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

June 18th, 2012, 04:02 PM
|
|
|
|
Rewrite to different kinds of php conditions
How would I got about using mod_rewrite to rewrite:
www.domain.com/index.php?home to www.domain.com/home
but also have
www.domain.com/index.php?profile=John to www.domain.com/John
|

June 18th, 2012, 07:15 PM
|
 |
Still alive
|
|
Join Date: Mar 2007
Location: Washington, USA
|
|
|
What if someone uses the name "home"?
|

June 18th, 2012, 08:25 PM
|
|
|
Quote: | Originally Posted by requinix What if someone uses the name "home"? |
That won't be permitted, we'll have a blocked list. Basically the users url will be in the DB, and that will be used to pull their profile. When someone chooses their url, it'll be checked for the words we're using to make the site work, and then throw an error if it matches one of those.
|

June 18th, 2012, 09:00 PM
|
 |
Wiser? Not exactly.
|
|
Join Date: May 2001
Location: Bonita Springs, FL
|
|
Quote: | Originally Posted by HDFilmMaker2112 That won't be permitted, we'll have a blocked list. |
What about future expansion, like if someone is using the name 'contest' and you wanted to use that as the slug for some new contest your hosting?
It would be better to separate them somehow, such as doing /profile/John for user profiles, or /p/John if you want it shorter.
If you do still want to go with that setup though, you might look into RewriteMap, or just rewrite all the urls to something like index.php?slug=blah and inside index.php decide if it is a profile name or a page name.
|

June 18th, 2012, 10:13 PM
|
|
|
Quote: | Originally Posted by kicken What about future expansion, like if someone is using the name 'contest' and you wanted to use that as the slug for some new contest your hosting?
It would be better to separate them somehow, such as doing /profile/John for user profiles, or /p/John if you want it shorter.
If you do still want to go with that setup though, you might look into RewriteMap, or just rewrite all the urls to something like index.php?slug=blah and inside index.php decide if it is a profile name or a page name. |
Looking in to Rewrite Map... I came up with this.
Code:
RewriteMap pages txt:/pagesmap.txt
RewriteRule ^([A-Za-z0-9_]+)$ /index.php?${pages:$1|NOTFOUND} [PT]
#At this point wouldn't everything be sent to index?page_name_here or a 404 page?
RewriteMap profile txt:/profilemap.txt
RewriteRule ^([A-Za-z0-9_]+)$ index.php?profile=${profile:$1|NOTFOUND} [PT]
How would I add in the profile call though? I guess I need some kind of Rewrite Condition, but I have no idea how to do that. I would need a condition that says if the first rewrite rule returns a NOTFOUND go to the second Rewrite Rule.
EDIT:
What about this:
Code:
RewriteMap pages txt:/pagesmap.txt
RewriteRule ^([A-Za-z0-9_]+)$ /index.php?${pages:$1|profile=$1} [PT]
The problem here is I have an else statement in my pages which show up as the default page if the value doesn't match any of the if/elseif statements.
Last edited by HDFilmMaker2112 : June 18th, 2012 at 10:23 PM.
|

June 18th, 2012, 10:44 PM
|
|
|
Now that I'm thinking about it some more, redoing the if/else statements with a matching condition probably does make sense:
I would just have to make this:
PHP Code:
/*Account Settings Page*/
elseif($_GET['settings']=="account"){
$switch="content";
$content.='
Account Settings
';
}
/*Privacy Settings Page*/
elseif($_GET['settings']=="privacy"){
$switch="content";
$content.='
Privacy Settings
';
}
/*Settings Home Page*/
elseif(isset($_GET['settings'])){
$switch="content";
$content.='
Settings
';
}
into something like this:
PHP Code:
/*Account Settings Page*/
elseif($_GET['p']=="settings" && $_GET['edit']=="account"){
$switch="content";
$content.='
Account Settings
';
}
/*Privacy Settings Page*/
elseif($_GET['p']=="settings" && $_GET['edit']=="privacy"){
$switch="content";
$content.='
Privacy Settings
';
}
/*Settings Home Page*/
elseif($_GET['p']=="settings"){
$switch="content";
$content.='
Settings
';
}
Then rewrite to /settings and /settings/account and /settings/privacy
Doing that it should be as simple as the following, right:
Code:
RewriteRule ^([A-Za-z0-9_]+)/?([A-Za-z0-9_]+)?/?$ /index.php?p=$1&edit=$2
That should match anything between a-Z and 0-9 with an optional / and an optional second "folder" with an optional /.
Last edited by HDFilmMaker2112 : June 18th, 2012 at 10:58 PM.
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|