sprintserve
06-24-2004, 01:54 PM
Hi
Need a bit of assistance here. How do you change
http://www.domain.com/dir/script.php?u=123&i=456
To something like
http://www.domain.com/dir/script/123/456.php
or something similar. What needs to be in the .htaccess
In short, going to the second URL is the interpreted as going to the first. Let me know if anyone can help.
Dan L
06-24-2004, 02:01 PM
Go into httpd.conf and add in a LoadModule line for mod_rewrite, and restart Apache.
Then, create a .htaccess file that looks like:
RewriteEngine On
RewriteBase /
RewriteRule cn/scripts/(.+)/?$ index.php?a=$1&b=scripts [L,NC]
RewriteRule cn/(.+)/?$ index.php?a=$1 [L,NC]
# Making sure we get the correct error page.
ErrorDocument 404 /cn/error.php
The first set turns on the rewrite engine, and rewrites two separate types of URLs. The second set is just to set the 404 page.
---
I didn't bother going in-depth since it's self-explanatory, but if something doesn't work, I'd be glad to expand on it.
---
Edit: Just to clarify:
RewriteRule cn/(.+)/?$ index.php?a=$1
The bolded ones are related variables, and the first underlined one is what the new URL is, and the second is what the URL is 'behind the scenes.'
sprintserve
06-24-2004, 02:11 PM
To be frank, I am not sure how to apply it in my example. Would you be able to provide the rules example exactly as it would apply to the example urls above? I have goggled and read many examples, but is still unsure and those I wrote and tested based on those examples don't work
So if it is not too much trouble can you rewrite using the variables, script name, directory etc. so that I can map it and learn from it. Thanks.
Dan L
06-24-2004, 02:22 PM
Sure.
----
Step 1: Open httpd.conf and find a free line.
Step 2: Add the following code: LoadModule rewrite_module modules/mod_rewrite.so
Step 3: Close httpd.conf and restart Apache.
Step 4: FTP into /dir/
Step 5: Create a file called .htaccess
Step 6: Edit .htaccess. Inside, put the following: RewriteEngine On
RewriteBase /
RewriteRule (.+)/(.+)/(.+)/?$ $1.php?u=$2&i=$3 [L,NC]
ErrorDocument 404 /script/123/error
Step 7: Save and close .htaccess.
Explanations:
- The URL will look like what you have, except without the .php extension. That is so if you change the programming language, you don't lose your search engine spots.
- The ErrorDocument line makes sure that any wrong URLs get redirected to a certain page.
----
If you're still confused about any step, just ask.
sprintserve
06-24-2004, 02:51 PM
Works like a charm! Thanks for the patience and excellent detailed explanation. Perhaps I should make this a How to in the How to forum.
One more question: Is it possible not to have the .php extension appear in the URL?
Dan L
06-24-2004, 09:57 PM
Yeah. The code I gave you should work fine without the .php extension. If it doesn't, can you show me an example?
As for a tutorial, if there aren't any mod_rewrite ones, just streamline my second post and your first one, and it should be fine.
acctman
06-29-2004, 05:09 AM
RewriteEngine On
RewriteBase /
RewriteRule test2/?$ profile.php?id=1 [L,NC]
that works but http://www.domain.com/test2/ doesn't load images on the page, http://www.domain.com/test2 works fine though
sprintserve
06-29-2004, 05:59 AM
DanX code works perfectly fine. I just didn't realised it. Thanks :)