简体   繁体   中英

I need a mod_rewrite expression to rewrite all paths that do not have a file extension

What i'm trying to do is have my main script index.php process all requests that do not contain a file extension. All other requests that do contain a file extension such as images should not be affected by mod_rewrite.

For example, I need the following urls to all be rewritten to /index.php?req={REQUEST_URI}:
/index/a/b/c
/index/a/b
/index

But disregard urls with an extension. Apache and not PHP should process urls such as:
/index/a/test.png
/index/a/b/c/script.js

My current expression only works for paths with a single directory.

RewriteRule ^/?([a-z0-9]+)$ /index.php?req=%{REQUEST_URI} [QSA,L]

This expression rewrites "/index", but not "/index/a" and so on...

Any help would be greatly appreciated. Thanks!

You can use:

RewriteRule !^/.*\.([a-zA-Z0-9]){2,}$ /index.php?req=%{REQUEST_URI} [QSA,L]

This redirects anything that does not match the pattern ^/.*\.([a-zA-Z0-9]){2,}$ . The pattern defines a string ending with a period \. followed by two or more alphanumeric characters ([a-zA-Z0-9]){2,} .

If you know the length of your file extensions, you can modify the numbers in the curly braces; eg file extensions between 1 and 3 characters would be satisfied with ([a-zA-Z0-9]){1,3}

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