antivinegar antivinegar - 1 year ago 161
Apache Configuration Question

htaccess Replace spaces and dots with a dash

Ok I've got my htaccess code to work up to a point... Now I'm stuck.

Here is the original URL:

What I'm trying to achieve is a clean URL with no dots / periods or spaces (%20) like this:

However, with the htaccess code I'm currently using I get this result:

The doctors name is pulled from a database and it has a dot (.) behind every "dr" so this is where the process stops in a sense. I'm guessing because of the dot?

Here is the htaccess code that I have:

RewriteEngine On

RewriteBase /search/

RewriteCond %{THE_REQUEST} /store_info\.php\?store=([a-z0-9]+)&dentist= ([a-z0-9]+) [NC]
RewriteRule ^ %1/%2/? [R=301,L]

RewriteRule ^([a-z0-9]+)/([a-z0-9]+)/?$ store_info.php?store=$1&dentist=$2 [QSA,L,NC]

Answer Source

You can use these rules:

Options -MultiViews
RewriteEngine On    
RewriteBase /search/

# redirect internal URL to pretty URL
RewriteCond %{THE_REQUEST} /store_info\.php\?store=([a-z0-9]+)&dentist=([^\s&]+) [NC]
RewriteRule ^ %1/%2/? [L,NE,R=301]

# skip all files and directories from rules below
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]

# recursive rule to repeatedly convert DOT or space to hyphen
RewriteRule ^([a-z0-9]+)/([^\s.]*)[.\s]+(.*)$ $1/$2-$3 [NC,DPI,E=DONE:1]

# after all the hyphen conversion is done do a redirect
RewriteCond %{ENV:DONE} =1
RewriteRule ^([0-9a-z]+)/([^\s.]+)$ $1/$2 [R=301,NE,L]

# internally rewrite pretty URL to actual one
RewriteRule ^([a-z0-9]+)/([a-z0-9-]+)/?$ store_info.php?store=$1&dentist=$2 [QSA,L,NC]
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download