.htaccess-将不存在的文件从子目录重定向到根目录

.htaccess-将不存在的文件从子目录重定向到根目录,.htaccess,redirect,.htaccess,Redirect,当前目录结构: wwwpublic |+ spanish(language) |-- index.php |-- contact.php | |- index.php |- aboutus.php |- products.php |- contact.php 在wwwpublic目录的根目录中,我有英文文件。由于该网站也有西班牙语,所以我创建了一个名为“西班牙语”的单独目录。两个目录中的页面名称完全相同 现在,西班牙语目录中的一些页面不存在,我需要将这些页面的请求重定向到根文件夹,并保留请求的

当前目录结构:

wwwpublic
|+ spanish(language)
|-- index.php
|-- contact.php
|
|- index.php
|- aboutus.php
|- products.php
|- contact.php
在wwwpublic目录的根目录中,我有英文文件。由于该网站也有西班牙语,所以我创建了一个名为“西班牙语”的单独目录。两个目录中的页面名称完全相同

现在,西班牙语目录中的一些页面不存在,我需要将这些页面的请求重定向到根文件夹,并保留请求的文件名

例如:
访问者进入西班牙语版本,在那里打开index.php,然后单击西班牙语目录中的aboutus.php页面(不存在),然后.htaccess将其重定向到/root/aboutus.php

在您的
wwwpublic
目录中尝试类似的操作,最好在您拥有任何路由规则之前:

RewriteEngine On

# conditions to check that current request doesn't point to a valid resource
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

# condition to check that request is for the /spanish/ directory
RewriteCond %{REQUEST_URI} ^/spanish/(.+)$

# conditions to check if the /spanish/ is removed whether it would point to a valid resource
RewriteCond %{DOCUMENT_ROOT}/%1 -f [OR]
RewriteCond %{DOCUMENT_ROOT}/%1 -d

# all conditions match, rewrite
RewriteRule ^/?spanish/(.+)$ /$1 [L]

在您的
wwwpublic
目录中尝试类似的操作,最好在您拥有任何路由规则之前:

RewriteEngine On

# conditions to check that current request doesn't point to a valid resource
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

# condition to check that request is for the /spanish/ directory
RewriteCond %{REQUEST_URI} ^/spanish/(.+)$

# conditions to check if the /spanish/ is removed whether it would point to a valid resource
RewriteCond %{DOCUMENT_ROOT}/%1 -f [OR]
RewriteCond %{DOCUMENT_ROOT}/%1 -d

# all conditions match, rewrite
RewriteRule ^/?spanish/(.+)$ /$1 [L]