0

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
2
  • I would like to get answer from the downvoter... Sep 2, 2013 at 4:12
  • Define RewriteBase as per your domain. Sep 2, 2013 at 5:55

2 Answers 2

2
+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
8
0

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'
);
1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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