URL Rewrite & .htaccess Generator

Turn a dynamic address such as product.php?id=12&cat=4 into an Apache mod_rewrite rule and an nginx equivalent, and copy the usual snippets for www, HTTPS and trailing slashes.

Canonical host
Trailing slash

Pretty URL: /product/12/4

Apache .htaccess

# Paste into the site root .htaccess on Apache. Back up the current file first.
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
RewriteCond %{THE_REQUEST} \s/+product\.php\?id=([0-9]+)&cat=([0-9]+)\s [NC]
RewriteRule ^ /product/%1/%2? [R=301,L]
RewriteRule ^product/([0-9]+)/([0-9]+)/?$ /product.php?id=$1&cat=$2 [L,QSA]

nginx

# Put this in the nginx server block. nginx does not read .htaccess.
# In the port 80 server: return 301 https://$host$request_uri;
location = /product.php {
    if ($args ~ "^id=([0-9]+)&cat=([0-9]+)$") {
        return 301 /product/$1/$2;
    }
}
rewrite ^/product/([0-9]+)/([0-9]+)/?$ /product.php?id=$1&cat=$2 last;

Common .htaccess snippets

Force HTTPS

Sends every HTTP request to the same host on HTTPS.

RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
Force www

Sends the bare host to www and keeps the scheme. Turn on the HTTPS rule too if you want both.

RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ %{REQUEST_SCHEME}://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
Force non-www

Strips a leading www and keeps the scheme.

RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^ %{REQUEST_SCHEME}://%1%{REQUEST_URI} [R=301,L]
Add a trailing slash

Adds a slash when the address is not a real file. Don't combine this with the remove rule.

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !/$
RewriteRule ^(.+)$ /$1/ [R=301,L]
Remove a trailing slash

Removes a slash when the address is not a directory.

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)/$ /$1 [R=301,L]

Common nginx snippets

Force HTTPS

Put this in the server block that listens on port 80. nginx does not read .htaccess.

server {
    listen 80;
    server_name example.com www.example.com;
    return 301 https://$host$request_uri;
}
Force www

One server block answers for the bare host and redirects. The www block holds the site.

server {
    listen 443 ssl;
    server_name example.com;
    return 301 https://www.example.com$request_uri;
}
Force non-www

The www name redirects to the bare host.

server {
    listen 443 ssl;
    server_name www.example.com;
    return 301 https://example.com$request_uri;
}
Add a trailing slash

Skips addresses that look like files (anything with a dot in the last segment).

rewrite ^([^.]*[^/])$ $1/ permanent;
Remove a trailing slash

Strips one trailing slash. Don't use this on directory indexes you want to keep.

rewrite ^/(.*)/$ /$1 permanent;

Want this handled for you?

SEOchat writes about your business, keeps your Google listing active and reports every week. No contract.

How to turn a query string into a path

  1. Paste the address your script uses, with the parameters filled in. product.php?id=12&cat=4 becomes /product/12/4 unless you type your own pattern.
  2. A value that is all digits becomes a [0-9]+ pattern. Anything else becomes "one path segment". The sample values are only an example of the shape.
  3. Copy the Apache block into the site root .htaccess, or the nginx block into the server config. Then request the old URL and the new one.

Test the rules before you leave them on

The 301 only matches the parameter names in the order you entered, and it drops the query string so the pretty URL stays clean. Extra parameters, or the same parameters in another order, will not match that redirect. The internal rewrite still runs the script and keeps any extra query string it is given.

After you publish the file, follow the old URL with the redirect checker and confirm the status and the Location header with the HTTP header checker. If the same page answers on two addresses, set a canonical with the meta tag analyzer as a backstop.

Rules run in the browser. Nothing you type is sent to a server.

Questions

Does a pretty URL rank better by itself?

No. Google has described URL structure as a minor signal. A readable address helps people and is easier to share. Rankings come from the page, not from hiding the question mark.

Will this rule break my site if I paste it?

It can, if it fights a rule you already have. On Apache, back up .htaccess and test one URL. On nginx, .htaccess does nothing: the nginx snippet goes in the server block, then you reload nginx. WordPress and most site builders already rewrite URLs and will ignore or overwrite a hand-written rule.

Why are there two rules for one URL?

The rewrite makes /product/12/4 run product.php. The redirect sends anyone who opens the old query-string URL to the pretty one with a 301, so search engines don't keep two addresses. The redirect reads the original browser request, which is what stops the two rules from looping.

Do I need both a www rule and a non-www rule?

No. Pick one host and send the other to it. Also send HTTP to HTTPS. A visitor should land on one version of the address in a single hop.

What does the trailing-slash snippet do?

One version adds a slash to addresses that aren't files. The other removes a slash from addresses that aren't folders. Pick one. The dynamic rule allows either, so the slash choice still matches.

More free tools

See all tools