sam nikzad
sam nikzad

Reputation: 1360

Replacing space from special urls using htaccess

I'm able to replace all %20 with - from url by using :

.htaccess :

RewriteRule "^(\S*)\s+(\S*)$" /$1-$2 [L,NE,R=302]
RewriteRule "^(\S*)\s+(\S*\s+.*)$" $1-$2 [L]
# remove multiple hyphens
RewriteRule ^(.*)-{2,}(.*)$ /$1-$2 [L,R=302]

Now i want to do this work for only desired urls Not all , url's like like :

Example.com/blog/example%title => example.com/blog/example-title

Example.com/product/example%product => example.com/product/example-product

How i can do this using .htaccess ?

EDIT :

Main Problem is that when i upload images to server with % in name like image 1.jpg url will redirect me to a name like image-1.jpg and server can't find image to show

EDIT 2

my .htaccess have this codes beforehand:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
RewriteCond %{HTTPS} off
RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

Upvotes: 1

Views: 58

Answers (1)

anubhava
anubhava

Reputation: 785128

You can place known prefixes in your rules:

RewriteRule "^((?:blog|product)/\S*)\s+(\S*)$" /$1-$2 [L,NE,R=302,NC]

RewriteRule "^((?:blog|product)\S*)\s+(\S*\s+.*)$" $1-$2 [L,NC]

# remove multiple hyphens
RewriteRule ^((?:blog|product)/.*)-{2,}(.*)$ /$1-$2 [L,R=302,NE,NC]

Upvotes: 1

Related Questions