For example, I have an URL that looks for an image like this:
例如,我有一个URL来查找这样的图像:
http://example.com/img/foo.png
http://example.com/img/interface/menu/bar.png
http://example.com/static/users/avatars/small/3k5jd355swrx221.jpghttp://example.com/img/foo.png http://example.com/img/interface/menu/bar.png http://example.com/static/users/avatars/small/3k5jd355swrx221.jpg
I don't want to redirect those. They should just pass through. But then, I have URLs like this:
我不想重定向那些。他们应该通过。但是,我有这样的网址:
http://example.com/register/
http://example.com/my_account/my_picture/
http://example.com/contact/email/http://example.com/register/ http://example.com/my_account/my_picture/ http://example.com/contact/email/
All such URLs that don't request for an .png or .jpeg should be redirected to:
所有不请求.png或.jpeg的此类网址都应重定向到:
Where x stands for everything after example.com/, so in this example for example:
其中x代表example.com/之后的所有内容,因此在此示例中例如:
http://example.com/register/ to
http://example.com/index.php/register/http://example.com/register/到http://example.com/index.php/register/
http://example.com/my_account/my_picture/ to
http://example.com/index.php/my_account/my_picture/http://example.com/my_account/my_picture/至http://example.com/index.php/my_account/my_picture/
http://example.com/contact/email/ to
http://example.com/index.php/contact/email/http://example.com/contact/email/访问http://example.com/index.php/contact/email/
(AcceptPathInfo is enabled)
(AcceptPathInfo已启用)
Is there any way to do that in the .htaccess? I only know how I could do this if I had always something like http://example.com/someKindOfMarkerHere/stuff/stuff/stuff but I don't want to have the someKindOfMarker there to detect if it's an URL that has to be rewritten. I don't know how to exclude them.
在.htaccess中有没有办法做到这一点?我只知道如果我总是像http://example.com/someKindOfMarkerHere/stuff/stuff/stuff这样做,但我不想让someKindOfMarker在那里检测它是否是必须的URL重写。我不知道如何排除它们。
6 个解决方案
#1
You can either exclude specific URLs:
您可以排除特定网址:
RewriteEngine on
RewriteCond %{REQUEST_URI} !^/index\.php$
RewriteRule !.*\.(jpeg|png)$ index.php%{REQUEST_URI}
Or you exclude any existing file:
或者您排除任何现有文件:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php%{REQUEST_URI}
#2
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} ^.+\.png$ [OR]
RewriteCond %{REQUEST_FILENAME} ^.+\.jp(e)?g$
RewriteRule ^.*$ - [NC,L]
RewriteRule ^.*$ index.php/%{REQUEST_URI} [NC,L]
#3
Hell yes it's possible.
地狱是的,这是可能的。
mod_rewrite will do all of that for you pretty easily.
mod_rewrite很容易为你完成所有这些。
You can also set up an error handler, so every 404 on your site gets redirected through index.php. This is a nice little way of making sure all requests load index.php (or your bootstrap).
您还可以设置错误处理程序,以便通过index.php重定向站点上的每个404。这是确保所有请求加载index.php(或您的引导程序)的一个很好的小方法。
The mod_rewrite will need a regex and regex's hurt my head, so I'll let somebody else write one.
mod_rewrite需要一个正则表达式并且正则表达式会伤害我的头脑,所以我会让其他人写一个。
Hope that helps. Just comment if you need more info from me. :)
希望有所帮助。如果您需要我的更多信息,请发表评论。 :)
#4
Put something like this in a .htaccess file and make sure mod_rewrite is enabled:
把这样的东西放在.htaccess文件中并确保启用了mod_rewrite:
RewriteEngine On
RewriteRule ^(.*?)(?!(\.png|\.jpg))$ index.php/$1
#5
I would use a slight variation to Gumbo's answer:
我会对Gumbo的答案略有不同:
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php%{REQUEST_URI}
It excludes folders as well as files (the !-d flag) - you may not may not want this, but think it belongs here for completeness.
它排除文件夹和文件(!-d标志) - 你可能不会想要这个,但认为它完全属于这里。
#6
The following ignores existing files, folder and files with the extension matching: jpg, jpeg, png, gif. If you wish to add additional extension, just add "|extension" before the )$ on line 3.
以下内容忽略扩展名匹配的现有文件,文件夹和文件:jpg,jpeg,png,gif。如果您想添加额外的扩展名,只需在第3行的$之前添加“| extension”。
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !.(jpg|jpeg|png|gif)$
RewriteRule ^(.*)?$ /index.php/$1 [QSA,L]
#1
You can either exclude specific URLs:
您可以排除特定网址:
RewriteEngine on
RewriteCond %{REQUEST_URI} !^/index\.php$
RewriteRule !.*\.(jpeg|png)$ index.php%{REQUEST_URI}
Or you exclude any existing file:
或者您排除任何现有文件:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php%{REQUEST_URI}
#2
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} ^.+\.png$ [OR]
RewriteCond %{REQUEST_FILENAME} ^.+\.jp(e)?g$
RewriteRule ^.*$ - [NC,L]
RewriteRule ^.*$ index.php/%{REQUEST_URI} [NC,L]
#3
Hell yes it's possible.
地狱是的,这是可能的。
mod_rewrite will do all of that for you pretty easily.
mod_rewrite很容易为你完成所有这些。
You can also set up an error handler, so every 404 on your site gets redirected through index.php. This is a nice little way of making sure all requests load index.php (or your bootstrap).
您还可以设置错误处理程序,以便通过index.php重定向站点上的每个404。这是确保所有请求加载index.php(或您的引导程序)的一个很好的小方法。
The mod_rewrite will need a regex and regex's hurt my head, so I'll let somebody else write one.
mod_rewrite需要一个正则表达式并且正则表达式会伤害我的头脑,所以我会让其他人写一个。
Hope that helps. Just comment if you need more info from me. :)
希望有所帮助。如果您需要我的更多信息,请发表评论。 :)
#4
Put something like this in a .htaccess file and make sure mod_rewrite is enabled:
把这样的东西放在.htaccess文件中并确保启用了mod_rewrite:
RewriteEngine On
RewriteRule ^(.*?)(?!(\.png|\.jpg))$ index.php/$1
#5
I would use a slight variation to Gumbo's answer:
我会对Gumbo的答案略有不同:
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php%{REQUEST_URI}
It excludes folders as well as files (the !-d flag) - you may not may not want this, but think it belongs here for completeness.
它排除文件夹和文件(!-d标志) - 你可能不会想要这个,但认为它完全属于这里。
#6
The following ignores existing files, folder and files with the extension matching: jpg, jpeg, png, gif. If you wish to add additional extension, just add "|extension" before the )$ on line 3.
以下内容忽略扩展名匹配的现有文件,文件夹和文件:jpg,jpeg,png,gif。如果您想添加额外的扩展名,只需在第3行的$之前添加“| extension”。
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !.(jpg|jpeg|png|gif)$
RewriteRule ^(.*)?$ /index.php/$1 [QSA,L]