Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I am struggling to create custom rewrite rules for wordpress.

This is my url structure

http://domain/cityprofile/?city=Sydney 

where city profile is a page template.

Now i want to make this url as

http://domain/Sydney 

.htaccess

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

# END WordPress
share|improve this question
    
I would like to get answer from the downvoter... –  Pramod Sep 2 '13 at 4:12
    
Define RewriteBase as per your domain. –  Bindiya Patoliya Sep 2 '13 at 5:55
add comment

2 Answers

up vote 2 down vote accepted
+50

[answer updated as wanted]

It's better to hook your function to generate_rewrite_rules so you won't have fall into infinite loops and flushing rewrite rule on every request. So you can do this:

function add_my_rules($wp_rewrite) 
{
    $new_rules = array(
        '^cityprofile/\?city=(Sydeny|Melbourne|Brisbane)$' => $wp_rewrite->preg_index(1) // for 3 cities
    );
    $wp_rewrite->rules = $new_rules + $wp_rewrite->rules;
}
add_action('generate_rewrite_rules', 'add_my_rules');

and then regenerate structure by going to Permalinks structure section and doing an update.

Note that this will redirect http://domain.com/cityprofile/?city=Sydneyto http://domain.com/Sydney but if you mean redirection from second URL to the first one I should say that it's not possible because it doesn't have somthing particular and clear in url to write a specefic pattern and will effect all other urls as well.

So my suggestion:

You can make your url more structural and understandable by adding a rewrite rule for http://domain.com/city/Sydney that opens up http://domain.com/cityprofile/?city=Sydeny

Update #2

Replace this rules with previous ones in .htaccess:

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^(Sydney|Melbourne|Brisbane)$ cityprofile/?city=$1
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

# END WordPress
share|improve this answer
    
Still no luck dear :-( not working for fca.siteprogress.info/Sydney –  Pramod Aug 30 '13 at 10:44
    
What can be wrong here mate, is there any server issue that not allowing this. –  Pramod Aug 30 '13 at 10:49
    
@Pramod I updated again. replace all lines again –  revo Aug 30 '13 at 10:53
    
Nothing happen dear... –  Pramod Aug 30 '13 at 11:00
1  
@Pramod hmmm so it's not about htaccess rules, I tested it here martinmelin.se/rewrite-rule-tester it works. –  revo Aug 30 '13 at 11:04
show 4 more comments

First of all, you do know that this will collide with pages (and when having permalink set to %postname%, posts as well) that have the same name as some city, right?

That being said, put the following in your functions.php file:

add_rewrite_rule(
    '^cityprofile/(.*)city=([^&]*)(.*)',
    '$matches[2]/$matches[1]$matches[3]',
    'top'
);

What does it do?

  • take an URL that is
    • starting with cityprofile/
    • having the city query var set
  • rewrite the URL as desired
  • account for other query vars that might be present

If you don't want to account for other query vars the following should do already:

add_rewrite_rule(
    '^cityprofile/(.*)city=([^&]*)',
    '$matches[2]',
    'top'
);
share|improve this answer
    
Its still not working fca.siteprogress.info/Sydney –  Pramod Aug 30 '13 at 6:59
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.