的.htaccess,mod_rewrite的,正则表达式用目录和文件类型(.htaccess, mod_rewrite, regex with directory and filetype)

   IT问题网   2018-12-07 00:00:00

问 题

我想做一个重写具备以下条件:

  1. 目录是/图片
  2. 在文件中有一个jpg格式,png格式或.gif扩展名

我要重定向到以下

/images/file=filename.extension
 

这是行不通的:

 重写规则/images/(.*\.jpg|png|gif)/图片/?文件= $ 1
 

例如:

/images/example.jpg=>/images/file=example.jpg

感谢

解决方案

在.htaccess文件,该路径被剥离的斜线,所以你需要开始使用图片/ 而不是 /图片/ 。此外,您还可以用开始 ^ 匹配路径的开头,这样像 /blahblah/images/something.gif 将不会被改写。最后,你的括号将匹配 foo.jpg 而不是 foo.png 或 foo.gif 。试试这个:

 重写规则^图像/(+ \(jpg |。png | gif))?/图片/文件= $ 1
 

标签:正则表达式目录以及文件类型



分享:

  • 微信
  • QQ好友
  • QQ空间
  • 新浪微博


热门推荐

.htaccess的语言重定向310错误(Htaccess language Redirect 310 error)

problem i have a two language site swedish sidewww.site ...

重定向一切的index.php(Redirection everything to index.php)

problem i wan't to redirect everything to index.php exepe ...

生成SE友好的URL的.htaccess国防部重写(Generating SE friendly URL .htaccess mod rewrite)

problem i've a url which get file name through get method ...

htaccess的子域名重定向(htaccess subdomain redirect)

problem i've searched around and tried a few ...

UrlRewrite 3 GET请求(UrlRewrite with 3 get request)

problem i'm working on a project and stuck on urlrewrite. ...

mod_rewrite的:恶意重定向(Mod_ReWrite: Unwanted redirect)

problem possible duplicate: .htaccess, clean ...

限制用户子目录(limiting users to subdirectories)

problem i have a subdirectory of users that i want to lim ...

重定向子文件夹的.htaccess(Redirection Subfolder Htaccess)

problem i want a redirect using htaccess i have a post na ...

使用的.htaccess,从来没有表现出的index.php(Using .htaccess, never show index.php)

problem how can i never show index.phpfor example, all re ...

在子域的内部服务器错误(internal server error in subdomains)

problem i installed worpress to my main domain abcd.com. ...

基于用户代理手机网站重定向(Mobile website redirection based on user agent)

problem say i've got two distinct domains: ...

的Joomla htaccess的不工作(Joomla htaccess not working)

problem i am using joomla 1.7 and using joomla's built in ...

扩大友好的URL,国防部重写(Expanding friendly url, mod rewrite)

problem i am using modx, which is a cms system. it has a ...

共享主机Zend的项目?(zend project on shared hosting )

problem i created a zend project on localhost on ubuntu o ...

URL结构的htaccess(url construction htaccess)

problem lets say i have a link to one of my page that loo ...

htaccess的重写规则(.htaccess RewriteRule)

problem i'm trying to write a rewriterule (with no luck) ...

如何配置.htaccess文件的重写规则摆脱扩展,尾随斜线,WWW?(How to configure .htaccess file for rewrite rules to get rid of extensions, trailing slashes, www)

problem this is what i want - when somebody enters site. ...

htaccess的基础上与通配符和东西后,到index.php index.php页面重定向不存在的页面(.htaccess redirect non existing pages based on index.php page with wildcard and something after it to index.php)

problem please help, some website is arbitrar ...

国防部重写的子域子域目录(mod-rewrite subdomain to subdomain dir)

problem i have a bit of a problem with rewrite rules; so ...

如何使用的.htaccess忽略/删除/页/ X /从网址是什么?(How to use .htaccess to ignore/remove /page/X/ from url)

problem how can i use .htaccess to strip/ignore /page/x/ ...