user3733831
user3733831

Reputation: 2926

Rewrite rule is not working properly

I am trying to write SEO friendly usls using Apache Mod-Rewrite.

My URLs are something like this.

index.php?p=edit-profile 
index.php?p=edit-profile&id=3 
index.php?p=edit-profile&id=3&city=sydney
index.php?p=edit-profile&id=3&city=sydney&name=some example text

This is how I tried it.

RewriteRule ^([\w-]+)/?$ index.php?p=$1 [L,QSA,NC]
RewriteRule ^([\w-]+)/([\w-]+)/?$ index.php?p=$1&id=$2 [L,QSA,NC]
RewriteRule ^([\w-]+)/([\w-]+)/([\w-]+)/?$ index.php?p=$1&id=$2&city=$3 [L,QSA,NC]
RewriteRule ^([\w-]+)/([\w-]+)/([\w-]+)/([\w-]+)/?$ index.php?p=$1&id=$2&city=$3&name=$4 [L,QSA,NC]

Here, first, second and third rules are working for me. But my problem is If I have a string with spaces for fourth variable my forth rule is not working. And also if I have a single word for forth one again it doesn't work.

Can anybody tell me how I fix this problem. Is there a way to replace these spaces with hyphens?

Thank you.

Upvotes: 1

Views: 34

Answers (1)

anubhava
anubhava

Reputation: 785128

You can include space in your character class:

RewriteRule ^([\w-]+)/?$ index.php?p=$1 [L,QSA,NC]
RewriteRule ^([\w-]+)/([\w-]+)/?$ index.php?p=$1&id=$2 [L,QSA,NC]
RewriteRule ^([\w-]+)/([\w-]+)/([\w-]+)/?$ index.php?p=$1&id=$2&city=$3 [L,QSA,NC]
RewriteRule ^([\w-]+)/([\w-]+)/([\w-]+)/([\s\w-]+)/?$ index.php?p=$1&id=$2&city=$3&name=$4 [L,QSA,NC]

Upvotes: 1

Related Questions