简体   繁体   中英

Htaccess rule, convert dash to slash in url

I want to convert single (first) dash into slash. I have two urls here:

Actuall url: http://localhost/wordpress/accessories-car-alarm-system .

Should redirect to http://localhost/wordpress/accessories/car-alarm-system .

I want to convert this specific url from first one to second one. I am using this code but it doesn't work properly.

Whole htaccess file:

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /wordpress/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /wordpress/index.php [L]
RewriteRule ^/?accessories/(.+)$ /accessories-$1 [L,NC]
</IfModule>

# END WordPress  

Tried this too:

RewriteRule ^/?accessories/(.+)$ /accessories-$1 [L,NC]

Still not working

You can not use regex in rewrite RewriteTarget ,try

RewriteRule ^/?accessories/(.+)$ /accessories-$1 [L,NC]

Reference : https://httpd.apache.org/docs/current/mod/mod_rewrite.html

Use:

RewriteEngine on 
RewriteRule ^accessories-(.+)$ accessories/$1 [NC,R=301,L]

Without the / at the beginning in .htaccess

不是吗

RewriteRule ^/accessories-(.*)$ /accessories/(.*)$

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