简体   繁体   中英

How do I .htaccess redirect a url to include a folder and subfolder after the domain and before the filename?

So I'm trying to write an .htaccess line that redirects urls to the url including the category and subcategory. The filename repeats with a date on the end, and I don't want to disrupt other filenames coming after the domain.

https://example.com/filename-Sep-30 

https://example.com/filename-Oct-01

https://example.com/filename-Oct-02 

https://example.com/filename-Oct-03 

to

https://example.com/folder/subfolder/filename-Sep-30

https://example.com/folder/subfolder/filename-Oct-01

https://example.com/folder/subfolder/filename-Oct-02

https://example.com/folder/subfolder/filename-Oct-03

without affecting https://example.com/anotherfilename

I tried

<IfModule mod_rewrite.c>
RewriteEngine On
ReWriteRule ^/filename(.*)$ https://example.com/folder/subfolder [END,R=301]
</IfModule>

But it doesn't garner even a response from the page when loaded.

I changed the link structure for my site on wordpress, adding categories and sub categories and now the search engine is sending traffic to the old pages with 404 erors.

I thought Wordpress would automatically redirect the pages but that is not the case.

Try this :

RewriteEngine On
RewriteBase /
RewriteRule ^(filename-[a-zA-Z]+)(-[0-9]+)$  folder/subfolder/$1$2 [R=301,L,NE]

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