|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
Stop making mediocre tutorials.The best tutorials are video! Camtasia Studio makes it easy to create engaging, buzz-building screen videos at any size, in any popular format. Download the free trial!
|
|
#1
|
||||
|
||||
|
mod_rewrite - Create easier to read URL from longer link.
I was sent over here to ask this forum as I hear this is an Apache question. I don't know anything really about messing with Apache and don't want to mess up anything in httpd.conf
I was told mod_rewrite is what I need. I am hosted with westhost and seems that mod_rewrite is already set up. I have these dynamically created pages for each staff member. like this. http://www.domain.com/staff/results.php?NameID=1 of course each staff has a different name id #. They have been asking if there is an easier to read URL to tell their friends. For example: http://www.domain.com/JohnSmith that would forward to the correct staff id page for that name. Of course John Smith is first and last name of NameID #1. I have set up the .htaccess file. so far just this RewriteEngine On I assume I need something like this RewriteRule ^([^/]+)?$ staff/results.php?name1=$1 But I don't really understand what is happening or what I need to put there exactly. How does it grab the info from the database and know that John Smith goes to NameID #1? Thanks in advance for your help. |
|
#2
|
||||
|
||||
|
So you want to be able to use the full name instead of the unique ID?
Are the names always going to be unique? Probably not, and that's why you usually use the ID. Look at the MOD REWRITE sticky for "search on 404" (I believe it's Example 3) to see how to do the rewrite portion correctly since that's basically what you're doing. You will then need to modify results.php to search the database based on the name that's been passed instead of the ID that's been passed.
__________________
# Jeremy Explain your problem instead of asking how to do what you decided was the solution. |
|
#3
|
||||
|
||||
|
Quote:
Thanks, I think I actually figured this out! Pretty cool I must say. I have my .htaccess file in the root folder and is this. Code:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ staff/results.php?LastName=$1 [L]
I went with LastName for now as all of my staff (only 41 so far) have different names. I wasn't sure how to separate the REQUEST_FILENAME into the 2 names if someone did www.domain.com/SmithJohn And yes, I changed the code in results.php page to work properly. The only thing now is if there is an actual 404 error page needed for a file or folder that doesn't exist how do I get it to forward to the error page instead of going to the results page? For example, if someone enters www.domain.com/blah Obviously blah is not someones last name. and doesn't exist as a file/folder. That needs to forward to an error page doens't exist page. Any ideas. Otherwise I am pretty much there! Thanks again. |
|
#4
|
||||
|
||||
|
I would suggest doing that in results.php. The URL is considered user input, so it should be validated like any other user input.
The details would vary based on your implementation, but you could check the database for the last name and use PHP's header() to send the appropriate 404 response and redirect to a 404 webpage. The other approach is to move your .htaccess file into a folder and change the links to domain.com/folder/lastname. This would allow basic 404 handling for domain.com/foo, but you'd still need to address domain.com/folder/foo in results.php. |
![]() |
| Viewing: Dev Shed Forums > System Administration > Apache Development > mod_rewrite - Create easier to read URL from longer link. |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|
|