Вопрос

I have a problem with mod_rewrite.

I want to redirect those URIs in the title. I use the following rule

RewriteEngine on

RewriteCond $1 !^(folders not to be redirected e.g. css|images)
RewriteCond $1 !^(.*).(file extension e.g. png|css|txt|php)

RewriteRule ^(.*)$ index.php?id=$1 [L]

It work only if I put all the resources in a folder, else it would tell me:

"/foo/index.php" not found.

So to solve that I put all the resources in the folder "www"

But when I try to load the resources from a subfolder of e.g. "foo" it tells me:

The requested URL "/foo/foo2" was not found on this server.

How can I load resources from a subfolder like "/foo/foo2" or even "/foo/foo2/foo3"?

And how can I solve the problem with the automatic search for index.php in a folder?

Это было полезно?

Решение

I believe you can use the following to achieve your desired result. It doesn't filter by file extension, but rather checks to see if the file actually exists. One thing that is a little different is that it first appends a trailing slash to your links, something you may not want.

RewriteEngine on
RewriteBase /

# This appends a trailing slash. You will have to update http://so with your domain.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ http://so/$1/ [L,R=301]

# This does the internal redirect
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)/$ /index.php?id=$1 [L]

If you don't want the tailing slashes, you could use the following

RewriteEngine on
RewriteBase /

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ /index.php?id=$1 [L]
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top