htaccess如何将404重定向到变量url而不会导致无限循环?(htaccess how to redirect 404 to a variable url without causing infinite loop?)

我有一个站点,当它在URL的第一个相对路径上出现404错误时,我希望它立即重定向到相对于用户键入的路径。

所以例如输入是:site.com/input我希望这个第一级url字符串重定向到:site.com/m/input

我正在尝试这样的东西,它可以工作,但会导致无限循环:

RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d # RewriteRule . /index.php [L] RewriteRule ^ http://www.website.com/m%{REQUEST_URI} [R,L]

所以基本上,我希望它只触发一次,如果/ m / {variable url}的url不存在,那么我希望它落在实际的404页面或wordpress默认的RewriteRule上。 /index.php [L]

这可能吗? 非常感谢,莱昂

I have a site when it lands on a 404 error on the first relative path of the url I want it to instantly redirect to a path relative to what the user typed.

So for instance input was: site.com/input I want this first level url string to redirect to: site.com/m/input

I'm trying something like this, which works but causes an infinite loop:

RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d # RewriteRule . /index.php [L] RewriteRule ^ http://www.website.com/m%{REQUEST_URI} [R,L]

So basically, I want it to only fire once, and if the url at /m/{variable url} does not exist, then I want it to land on the actual 404 page or wordpress default's RewriteRule . /index.php [L]

Is this possible? Many thanks, Leon

最满意答案

如果已经是/m/ URL,则添加另一个不重定向的条件:

RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_URI} !^/m/ RewriteRule ^ http://www.website.com/m%{REQUEST_URI} [R,L]

Add another condition to not redirect if it's already an /m/ URL:

RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_URI} !^/m/ RewriteRule ^ http://www.website.com/m%{REQUEST_URI} [R,L]htaccess如何将404重定向到变量url而不会导致无限循环?(htaccess how to redirect 404 to a variable url without causing infinite loop?)

我有一个站点,当它在URL的第一个相对路径上出现404错误时,我希望它立即重定向到相对于用户键入的路径。

所以例如输入是:site.com/input我希望这个第一级url字符串重定向到:site.com/m/input

我正在尝试这样的东西,它可以工作,但会导致无限循环:

RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d # RewriteRule . /index.php [L] RewriteRule ^ http://www.website.com/m%{REQUEST_URI} [R,L]

所以基本上,我希望它只触发一次,如果/ m / {variable url}的url不存在,那么我希望它落在实际的404页面或wordpress默认的RewriteRule上。 /index.php [L]

这可能吗? 非常感谢,莱昂

I have a site when it lands on a 404 error on the first relative path of the url I want it to instantly redirect to a path relative to what the user typed.

So for instance input was: site.com/input I want this first level url string to redirect to: site.com/m/input

I'm trying something like this, which works but causes an infinite loop:

RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d # RewriteRule . /index.php [L] RewriteRule ^ http://www.website.com/m%{REQUEST_URI} [R,L]

So basically, I want it to only fire once, and if the url at /m/{variable url} does not exist, then I want it to land on the actual 404 page or wordpress default's RewriteRule . /index.php [L]

Is this possible? Many thanks, Leon

最满意答案

如果已经是/m/ URL,则添加另一个不重定向的条件:

RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_URI} !^/m/ RewriteRule ^ http://www.website.com/m%{REQUEST_URI} [R,L]

Add another condition to not redirect if it's already an /m/ URL:

RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_URI} !^/m/ RewriteRule ^ http://www.website.com/m%{REQUEST_URI} [R,L]