/text-1--text-2-2 >> /?part1=text-1&part2=text-2-2

Discuss practical ways rearrange URLs using mod_rewrite.

/text-1--text-2-2 >> /?part1=text-1&part2=text-2-2

Postby Stas » Wed Feb 23, 2005 2:08 pm

Hello, please me help with mod_rewrite

I have now :

RewriteRule ^(.*)$ index.php?rurls=$1 [L]

/any-text >> index.php?rurls=anytext

but, is it possible with rewrite rule to create this thing?

url like /any-text-there-1--any-text-going-there-2/ ==> index.php?part1=any-text-there-1&part2=any-text-going-there-2

i want divide url into 2 parts through double hyphen,

please help.

Regards.
Stas
 

Postby Caterham » Wed Feb 23, 2005 3:31 pm

but, is it possible with rewrite rule to create this thing?
Yes,
Code: Select all
RewriteRule ^([a-z0-9-_]+)--([a-z0-9-_]+)/?$ /index.php?part1=$1&part2=$2 [L,NC]
RewriteRule ^([a-z0-9-_]+)$ index.php?rurls=$1 [L,NC]


avoid the regEx .*, if you can specify the allowed chars

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

Postby Stas » Fri Feb 25, 2005 1:26 am

Thank you very much, Robert.

but i have error 500
i am using apache on my localhost, windows.

before i had .htaccess:

DirectoryIndex index.php
Options FollowSymLinks
RewriteEngine on
#RewriteBase /

# EXISTING FILES
RewriteRule ^index.php index.php [L]
RewriteRule ^admin.php admin.php [L]
RewriteRule ^worddelete.php worddelete.php [L]
RewriteRule ^new_word.php new_word.php [L]
RewriteRule ^agent.php agent.php [L]
RewriteRule ^2.php 2.php [L]
RewriteRule ^login.php login.php [L]
RewriteRule ^settings.php settings.php [L]
RewriteRule ^install.php install.php [L]
# REWRITING THE OTHERS
RewriteRule ^(.*).html$ index.php?rurls=$1 [L]
RewriteRule ^(.*).htm$ index.php?rurls=$1 [L]
RewriteRule ^(.*)$ index.php?rurls=$1 [L]

but when i added :

RewriteRule ^([a-z0-9-_]+)--([a-z0-9-_]+)/?$ index.php?part1=$1&part2=$2 [L,NC]
RewriteRule ^([a-z0-9-_]+)$ index.php?rurls=$1 [L,NC]

- have got 500 error.


what i have to add / delete from htaccess to it get working ?
help, pls.
Thanks.
Stas
 

Postby Guest » Fri Feb 25, 2005 1:28 am

i just looked at Apache error log and see:

[Fri Feb 25 01:18:54 2005] [alert] [client 127.0.0.1] c:/appserv/www/sme/.htaccess: RewriteRule: cannot compile regular expression '^([a-z0-9-_]+)--([a-z0-9-_]+)/?$'
Guest
 

Postby Caterham » Fri Feb 25, 2005 4:25 am

This is a RegEx problem only for Apache 1.3

Just change the order of - and _:
Code: Select all
[a-z0-9-_]
into
Code: Select all
[a-z0-9_-]


lets optimize the rest of the code :)

Code: Select all
DirectoryIndex index.php
Options +FollowSymLinks
RewriteEngine on

# EXISTING FILES
RewriteRule ^(index|admin|worddelete|new_word|agent|2|login|settings|install)\.php - [L]

# REWRITING THE OTHERS
RewriteRule ^([a-z0-9_-]+)--([a-z0-9_-]+)/?$ index.php?part1=$1&part2=$2 [L,NC]
RewriteRule ^([^.]*)(\.(html?|php))?$ index.php?rurls=$1 [L]


Which URLs should match the last rule? This one matches to
/a.htm
/a.html
/a.php
/a
(also for subdirs /aa/bb/a.html etc.)
Caterham
 
Posts: 690
Joined: Fri Dec 10, 2004 1:30 pm

Postby Stas » Fri Feb 25, 2005 8:19 am

Thank you for help Robert,
it is working now :)
Stas
 


Return to Friendly URLs with Mod_Rewrite

Who is online

Users browsing this forum: No registered users and 51 guests

cron