重写规则并重写cond以在apache中重写URL

时间:2022-04-09 08:39:03

I am trying to get apache to rewrite cond/rules to rewrite the location of my bootstrap files which are located in a folder called /assets which is NOT in the root directory.

我试图让apache重写cond / rules来重写我的bootstrap文件的位置,这些文件位于名为/ assets的文件夹中,该文件夹不在根目录中。

I have added the rule:

我添加了规则:

RewriteCond $1 !^(index\.php|assets|)
RewriteRule ^(.*)$ /index.php?/$1 [L]

to try to catch a url such as:

试图抓住一个网址,如:

 <link rel="stylesheet" href="assets/css/app.css">

the full url the browser tries to find is:

浏览器尝试查找的完整网址是:

http://feeder.xxxxx.com/site/page/alert/v1/assets/plugins/jquery/jquery.min.js 

Which incorrectly has the REST/ information included in the url.

哪个错误地将URL /信息包含在网址中。

I need to divert it to:

我需要转移到:

http://feeder.xxxxx.com/templates/assets/css/app.css

I am currently using codeigniter (which I don't think makes any difference to this issue except for the MVC framework)

我目前正在使用codeigniter(除了MVC框架之外,我认为这个问题没有任何区别)

Sincere thanks for any help. It is greatly appreciated.

真诚的感谢您的帮助。非常感谢。

1 个解决方案

#1


I believe you need to use absolute URL for you rule to work, see below:

我相信你需要使用绝对URL来规则才能工作,见下文:

<link rel="stylesheet" href="/assets/css/app.css">

If you need to redirect /assets to /templates/assets then another rule is needed:

如果您需要将/ assets重定向到/ templates / assets,则需要另一条规则:

RewriteRule ^/?assets/(.+) /templates/assets/$1 [L]

The final .htaccess should look like:

最终的.htaccess应该是这样的:

RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond $1 !^(assets)
RewriteRule ^/?(.*)$ /index.php?/$1 [L]

# Redirect /assets to /templates/assets
RewriteRule ^/?assets/(.+) /templates/assets/$1 [L]

On the sidenote, presense of the question mark (?) after index.php doesn't seem right and looks like your application is misconfigured (maybe $config['enable_query_strings'] is set to TRUE). Please see CodeIgniter URLs for more information.

在旁注中,index.php之后的问号(?)的显示似乎不正确,看起来你的应用程序配置错误(可能$ config ['enable_query_strings']设置为TRUE)。有关更多信息,请参阅CodeIgniter URL。

#1


I believe you need to use absolute URL for you rule to work, see below:

我相信你需要使用绝对URL来规则才能工作,见下文:

<link rel="stylesheet" href="/assets/css/app.css">

If you need to redirect /assets to /templates/assets then another rule is needed:

如果您需要将/ assets重定向到/ templates / assets,则需要另一条规则:

RewriteRule ^/?assets/(.+) /templates/assets/$1 [L]

The final .htaccess should look like:

最终的.htaccess应该是这样的:

RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond $1 !^(assets)
RewriteRule ^/?(.*)$ /index.php?/$1 [L]

# Redirect /assets to /templates/assets
RewriteRule ^/?assets/(.+) /templates/assets/$1 [L]

On the sidenote, presense of the question mark (?) after index.php doesn't seem right and looks like your application is misconfigured (maybe $config['enable_query_strings'] is set to TRUE). Please see CodeIgniter URLs for more information.

在旁注中,index.php之后的问号(?)的显示似乎不正确,看起来你的应用程序配置错误(可能$ config ['enable_query_strings']设置为TRUE)。有关更多信息,请参阅CodeIgniter URL。