mod_rewrite 将某些文件夹转换为参数

发布于 2024-08-17 13:36:01 字数 2058 浏览 2 评论 0原文

我有一个网站,我想将第一个子文件夹转换为参数(来自一组子文件夹列表),因此

http://localhost/mysite/folder1/dosomething.php

显示如上所示,但对于 PHP 来说它看起来像:

... localhost/mysite/dosomething.php?organization=folder1

我只需要 URL 可能已附加或尚未附加参数

要在某些文件夹上执行此操作(例如,不是... localhost/mysite/admin/),我需要考虑到这样一个事实:我正在为其创建站点的 多个组织要使用,并且根据组织的不同,站点中的颜色等需要不同,但每个组织将使用相同的页面,并且客户希望 URL 中包含组织短代码。我认为最好的方法是使用 mod_rewrite。

我已经尝试过:

rewriterule ^folder1/(.*) $1&organisation=folder1 [NC]

但这不起作用,也无法处理 URL 可能已附加参数的事实。谁能建议一种方法来做到这一点?

亲切的问候和非常感谢

根据您的反馈:

嗯,我无法让它工作。在 .htaccess 我有 选项+FollowSymLinks 重写引擎开启 RewriteRule ^folder1/(.*) $1&organization=folder1 [NC,QSA]

如果我浏览 ... server/sitename/folder1/destination.php 我收到页面未找到错误,并且 htaccess 调试日志显示

:( 3) [perdir /var/www/sitename/] 添加路径信息后缀:/var/www/sitename/folder1 -> /var/www/sitename/folder1/destination.php (3) [perdir /var/www/sitename/] 去掉每个目录的前缀:/var/www/sitename/folder1/destination.php ->文件夹1/destination.php (3) [perdir /var/www/sitename/] 将模式 '^folder1/(.)' 应用到 uri 'folder1/destination.php' (2) [perdir /var/www/sitename/] 重写 'folder1/destination.php' -> 'destination.php&organization=folder1' (3) [perdir /var/www/sitename/] 添加每个目录前缀:destination.php&organization=folder1 -> /var/www/sitename/destination.php&organization=folder1 (2) [perdir /var/www/sitename/] 去掉 document_root 前缀:/var/www/sitename/destination.php&organization=folder1 -> /sitename/destination.php&organization=folder1 (1) [perdir /var/www/sitename/] 内部重定向 /sitename/destination.php&organization=folder1 [内部重定向] (3) [perdir /var/www/sitename/] 去掉每个目录的前缀:/var/www/sitename/destination.php&organization=folder1 ->目的地.php&组织=folder1 (3) [perdir /var/www/sitename/] 将模式 '^folder1/(.)' 应用到 uri 'destination.php&organization=folder1' (1) [perdir /var/www/sitename/] 传递/var/www/sitename/destination.php&organization=folder1

I have a site where I'd like to convert the first subfolder into a parameter (from a set list of subfolders), so

http://localhost/mysite/folder1/dosomething.php

is displayed as shown above, but to PHP it looks like:

... localhost/mysite/dosomething.php?organisation=folder1

I only need to do this on certain folders (i.e. not ... localhost/mysite/admin/ for example) and I need to take into account the fact that there may or may not already be parameters appended to the URL

I'm creating a site for multiple organisations to use, and based on the organisation, colours etc in the site will need to be different, but each organisation will be using the same pages, and the client wants the organisation shortcode in the URL. I thought the best way to do this would be with mod_rewrite.

I've tried:

rewriterule ^folder1/(.*) $1&organisation=folder1 [NC]

but this doesnt work or handle the fact there might already be parameters attached to the URL. Can anyone suggest a way to do this?

Kind regards and many thanks

Based on your feedback:

Hmm I can't get this to work. In .htaccess I have
Options +FollowSymLinks
RewriteEngine on
RewriteRule ^folder1/(.*) $1&organisation=folder1 [NC,QSA]

If I browse ... server/sitename/folder1/destination.php I get a page not found error, and the htaccess debug log says:

(3) [perdir /var/www/sitename/] add path info postfix: /var/www/sitename/folder1 -> /var/www/sitename/folder1/destination.php
(3) [perdir /var/www/sitename/] strip per-dir prefix: /var/www/sitename/folder1/destination.php -> folder1/destination.php
(3) [perdir /var/www/sitename/] applying pattern '^folder1/(.)' to uri 'folder1/destination.php'
(2) [perdir /var/www/sitename/] rewrite 'folder1/destination.php' -> 'destination.php&organisation=folder1'
(3) [perdir /var/www/sitename/] add per-dir prefix: destination.php&organisation=folder1 -> /var/www/sitename/destination.php&organisation=folder1
(2) [perdir /var/www/sitename/] strip document_root prefix: /var/www/sitename/destination.php&organisation=folder1 -> /sitename/destination.php&organisation=folder1
(1) [perdir /var/www/sitename/] internal redirect with /sitename/destination.php&organisation=folder1 [INTERNAL REDIRECT]
(3) [perdir /var/www/sitename/] strip per-dir prefix: /var/www/sitename/destination.php&organisation=folder1 -> destination.php&organisation=folder1
(3) [perdir /var/www/sitename/] applying pattern '^folder1/(.
)' to uri 'destination.php&organisation=folder1'
(1) [perdir /var/www/sitename/] pass through /var/www/sitename/destination.php&organisation=folder1

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

往日 2024-08-24 13:36:01

要将参数附加到查询字符串(而不是替换它们),请使用 QSA 标志:

RewriteRule ^folder1/(.*) $1&organisation=folder1 [NC,QSA]

“此标志强制重写引擎将替换字符串的查询字符串部分附加到现有字符串,而不是替换它。当您需要时使用此标志通过重写规则向查询字符串添加更多数据。”

请参阅 mod_rewrite 的 Apache 文档

To append parameters to the query string (instead of replacing them) use the QSA flag:

RewriteRule ^folder1/(.*) $1&organisation=folder1 [NC,QSA]

"This flag forces the rewrite engine to append a query string part of the substitution string to the existing string, instead of replacing it. Use this when you want to add more data to the query string via a rewrite rule."

See Apache documentation for mod_rewrite.

聽兲甴掵 2024-08-24 13:36:01

正确的规则是:

# rewrite for php scripts
RewriteRule ^(folder[0-9]+)(/([^/]*\.php)?)$ .$2?organisation=$1 [NC,QSA]
# rewrite for css and js (assuming they are relative to path of your htaccess)
RewriteRule ^(folder[0-9]+)/(.*) ./$2 [NC,QSA]

尽管如果您不介意将参数发送到任何地方,这也应该有效:

RewriteRule ^(folder[0-9]+)/(.*) $2?organisation=$1 [NC,QSA]

The correct rules would be:

# rewrite for php scripts
RewriteRule ^(folder[0-9]+)(/([^/]*\.php)?)$ .$2?organisation=$1 [NC,QSA]
# rewrite for css and js (assuming they are relative to path of your htaccess)
RewriteRule ^(folder[0-9]+)/(.*) ./$2 [NC,QSA]

Though this should work too if you don't mind sending your param everywhere:

RewriteRule ^(folder[0-9]+)/(.*) $2?organisation=$1 [NC,QSA]
哑剧 2024-08-24 13:36:01

我建议

RewriteRule ^folder1/(.*)?(.*) $1?organisation=folder1&$2 [NC]
RewriteRule ^folder1/(.*) $1?organisation=folder1 [NC]

我怀疑对正则表达式有点了解的人可以将这些组合成一条规则,

同时确保您通过

RewriteEngine on

在 .htaccess 中设置上面的重写规则来打开 mod 重写

I would suggest

RewriteRule ^folder1/(.*)?(.*) $1?organisation=folder1&$2 [NC]
RewriteRule ^folder1/(.*) $1?organisation=folder1 [NC]

I suspect someone who is a bit niftier with RegExs could combine these into one rule tho

Also make sure you have mod rewrite turned on by having

RewriteEngine on

above the rewrite rules in your .htaccess

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文