简体   繁体   中英

HTACCESS URL Variable Rewrite

Looking to rewrite a URL and can't seem to get it working.

Example URL:

http://www.example.com/find/agency.php?agency=33524&name=happy-example

Ideal, rewritten URL:

http://www.example.com/find/agency/happy-example

One of the many attempted rewrites:

RewriteRule ^/([.]+)/([.]+)$ agency.php?agency=$1&name=$2

RewriteRule ^/([.]+)/([.]+)/$ agency.php?agency=$1&name=$2

Even if the removal of the "agency=" variable isn't possible, I still can't seem to make this URL write out the variables - am I approaching the rewrite backwards?

Try using a rewrite condition to catch the query string before doing the rewrite.

For example something like:

RewriteCond %{QUERY_STRING} ^agency=33524&name=([^&]+)
RewriteRule ^find/agency.php$ find/agency/%1? [R=301]

There are several things wrong with your rule. However to get a URL like this.

http://www.example.com/find/33524/happy-example

You would need to use a rewrite like below.

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^find/([^/]+)/([^/]+)/?$ /agency.php?agency=$1&name=$2 [L]

Use a lazy modifier for the symbol matching (currently ([.]+) will match everything )

RewriteRule ^/([.]+?)/([.]+?)$ agency.php?agency=$1&name=$2
RewriteRule ^/([.]+?)/([.]+?)/$ agency.php?agency=$1&name=$2

You can also combine these into one rule

RewriteRule ^/([.]+?)/([.]+?)[/]{0,1}$ agency.php?agency=$1&name=$2

This does not include whatever flags you would need depending on your particular case

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM