Need Apache rewrite rule to change part of url

旧城冷巷雨未停 提交于 2020-01-07 03:00:33

问题


I have urls like this:

www.example.com/index.php/category-5/subcategory/page

I want to rewrite to:

www.example.com/category/subcategory/page

I.e. I want to remove the "index.php/" and also remove the "-5", but keep the rest.

I am in way over my head with Apache. I normally just write plain simple php code.

I tried this and it didn't work:

RewriteRule ^index.php/category-1/(.*)$ /category/($1) [NC]

Thanks

Also, please, if we could make it a permanent redirect that would be great.


回答1:


Don't use parenthesis in the replacement part:

RewriteRule ^index.php/category-1/(.*)$ /category/$1 [NC,R=301]

See:

http://httpd.apache.org/docs/current/mod/mod_rewrite.html#rewriteoptions

and

http://httpd.apache.org/docs/current/rewrite/flags.html

you could activate debug logs, I've always found this useful as RewriteRule are tricky:

RewriteLog "/path-to-log/rewrite.log"
RewriteLogLevel 4



回答2:


QUESTION:

www.example.com/index.php/category-5/subcategory/page

I want to rewrite to:

www.example.com/category/subcategory/page


You may try this in one .htaccess file in root directory:

Options +FollowSymlinks -MultiViews
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php/([^-]+)-[\d]+/([^/]+)/([^/]+)/? /$1/$2/$3  [R=301,NC,L]

Redirects permanently:

http://www.example.com/index.php/par1-AnyDigits/par2/par3

http://www.example.com/par1/par2/par3

Where par1, par2 and par3 are dynamic strings.

For internal mapping, replace [R=301,NC,L] with [NC,L]



来源:https://stackoverflow.com/questions/15512476/need-apache-rewrite-rule-to-change-part-of-url

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!