http://domain.com/users/jdoe/2004/12/5.html ->

Discuss practical ways rearrange URLs using mod_rewrite.

Postby Caterham » Mon Dec 27, 2004 11:13 am

for an automated rule, you should include the id into the URL like http://domain.com/users/jdoe-5/2004/12/5.html

for that you can use
Code: Select all
Rewriteengine on
RewriteRule ^users/([a-z]+)-([0-9]+)/.+\.html$ /etc/profile.php?user=$1&id=$2 [L]


Just place that code into a .htaccess in your doc root.

If you don't want (or don't need) to include the id into the URL, you can use
Code: Select all
RewriteRule ^users/([a-z]+)/.+\.html$ /etc/profile.php?user=$1&id=5 [L]


Bob
Caterham
 
Posts: 690
Joined: Fri Dec 10, 2004 1:30 pm

Postby ohyoko2 » Mon Dec 27, 2004 11:21 am

Thanks
ohyoko2
 

Postby ohyoko2 » Mon Dec 27, 2004 11:49 am

How would the rule (the first rule one you mentioned) differ if underscores were allowed in the username?
ohyoko2
 

Postby Caterham » Mon Dec 27, 2004 11:56 am

just change the RexularExpression to
([A-Za-z_]+)
If also capital letters might be displayed in the URL/username part, add A-Z
Caterham
 
Posts: 690
Joined: Fri Dec 10, 2004 1:30 pm

Postby ohyoko2 » Mon Dec 27, 2004 8:58 pm

I'm running Apache 1.3.9 on Win32; I will be running this on a commercial server running Apache 1.3.27 on Linux. First, I created a .htaccess file under http://localhost/myfiles/ (local path: C:\apache\htdocs\myfiles\). This entailed the following lines of code:

Code: Select all
RewriteEngine on
RewriteRule ^users/([0-9A-Za-z_]+)/+\.html$ /myfiles/etc/profile.php?user=$1&id=$2 [L]


Now, I shutdown Apache and started it back up again several times. Then, thinking it was the RewriteRule that was faulty, I even tried a few different commands -- namely:

Code: Select all
DirectoryIndex index.html


Now, even this didn't work. I'm not sure if I should be putting these files in some conf file (within the local path: C:\apache\conf\) instead, or what.

If you could help me straighten things out, I'd gladly appreciate it. Thank you much for your time, Caterham.
ohyoko2
 

Postby Caterham » Tue Dec 28, 2004 3:37 am

Watch out, you posted
Code: Select all
RewriteRule ^users/([0-9A-Za-z_]+)/+\.html$ /myfiles/etc/profile.php?user=$1&id=$2 [L]
Code: Select all
RewriteRule ^users/([0-9A-Za-z_]+)/.+\.html$ /myfiles/etc/profile.php?user=$1&id=$2 [L]

There is a Point missing, )/.+\.html$

So the whole URL called in a browser would be http://localhost/myfiles/users/jdoe/2004/12/5.html ?

Open httpd.conf, look for <Directory C:\apache\htdocs\> make sure, that
Code: Select all
AllowOverride FileInfo
or
Code: Select all
AllowOverride all
is set

I'm not sure if I should be putting these files in some conf file (within the local path: C:\apache\conf\) instead, or what
You can use mod_rewrite also in the httpd.conf (in <Directory C:\apache\htdocs\> ), but than you've to use
Code: Select all
RewriteRule ^/myfiles/users/([0-9A-Za-z_]+)/.+\.html$ /myfiles/etc/profile.php?user=$1&id=$2 [L]


Bob
Caterham
 
Posts: 690
Joined: Fri Dec 10, 2004 1:30 pm


Return to Friendly URLs with Mod_Rewrite

Who is online

Users browsing this forum: No registered users and 18 guests

cron