简体   繁体   中英

rewriterule for a set of URLs?

I would like to create a rewrite rule for .htaccess , with which I can redirect sub-pages. the rule should make the following thing:

I have hundreds of page which can be accessed under the following path

http://www.domain.de/firma/stadt/adresse

Now I would like to redirect all these URLs to

http://www.domain.de/firma/stadt

for that i used this rule:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^/firma/stadt/(.+)\.(htm|php|html)$ /firma/stadt/$1 [L,R=301]
</IfModule>

Unfortunately, nothing happens :-( Where is the mistake?

In addition, I would also like to exclude individual URLs from this rule ... Can someone help me ?

kind regards

You have an issue with the logic of your rewrite rule. Try this instead:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^firma/stadt/(.+)$ /firma/stadt/ [L,R=301]
</IfModule>

Reasons:

  1. according to your own description the URLs of the objects to not carry a "file name extension" in them http://www.domain.de/firma/stadt/adresse . So a rule expecting such will never match.

  2. the leading slash ( / ) in the mathcing pattern does not make any sense in .htaccess style rules, since those work on relative paths.

Also you have to take care of where to place that htaccess style file.

A general note: you should only use .htaccess style files if you really, really have to. A typical situation might be if you do not have access the the http servers configuration. Otherwise you should always prefer to place such rules inside the http servers host configuration. .htaccess style files are notoriously error prone, hard to debug and really slow the server down.

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