简体   繁体   中英

How to remove multi sub-directories from URL with .htaccess rewrite

I have an old website and I had changed my CMS site to WordPress.

Now I have many error 404 records in my Google Webmaster log.

I want to redirect an URL to a new structure...


Old URL:

www.mysite.com/fa/contents/detailXXXX/postslug

where XXXX is a 4-digit number, eg 4256

New URL:

www.mysite.com/postslug

A very simple solution would be:

RewriteEngine On
RewriteRule ^fa/contents/detail[0-9]{4}/postslug$ /postslug [R=301,L]

However if you need to know what digits were in the URL and use it in your script, use a cookie:

RewriteEngine On
RewriteRule ^fa/contents/detail([0-9]{4})/postslug$ /postslug [CO=n:$1:.mysite.com:0:/,R,L]

...or use a query parameter:

RewriteEngine On
RewriteRule ^fa/contents/detail([0-9]{4})/postslug$ /postslug?n=$1 [QSA,R,L]

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