使用DirectAdmin的Apache服务器 - .htaccess重写

3

我在设置重写时遇到了问题,希望有人知道答案。

这是我目前在 .htaccess 中使用的代码:

Options +FollowSymLinks
RewriteEngine on
RewriteBase /point/to/dir/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(\d+/.*)$ https://domain.ext/point/to/dir/module/status.php?id=$1 [L,QSA]

客户正在请求从服务器获取domain.ext/point/to/dir/,需要将其重写为https://domain.ext/point/to/dir/module/status.php。可能会在/point/to/dir/请求的末尾添加查询,应将其附加到https://domain.ext/point/to/dir/module/status.php。/point/to/dir/是服务器上不存在的位置。无论我做什么,都会出现内部服务器错误(.htaccess配置/语法不正确)或404错误。希望有人能帮忙。

欢迎来到SO,感谢您在问题中加入了自己的努力。关于“可能会将查询添加到/point/to/dir/请求的末尾,应将其附加到https://domain.ext/point/to/dir/module/status.php。/point/to/dir/是服务器上不存在的位置。”请说明您想要在URL中添加哪个查询字符串? - RavinderSingh13
非常抱歉,我的知識非常有限。可以添加任何內容到/point/to/dir/?。反斜杠(dir / )後的任何內容都應該附加到php - jexos
1个回答

3

根据您提供的示例,在.htaccess规则文件中按以下方式设置。请确保将路径从point/to/dir更改为实际路径(在规则前面不要放置/,否则规则将无效)。

这假定您的point/do/dir有模块文件夹和模块文件夹里的index.php。

在测试URL之前,请确保清除浏览器缓存。

Options +FollowSymLinks
RewriteEngine on
RewriteBase /point/to/dir/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(point/to/dir)/(.*)/?$ https://domain.ext/$1/module/status.php?id=$2 [NC,L,QSA]

或者如果您在相同的域名中,则不需要在重写规则的右侧提及完整的URL,只需像以下方式一样即可。请确保每次仅使用上述或以下规则之一。

Options +FollowSymLinks
RewriteEngine on
RewriteBase /point/to/dir/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(point/to/dir)/(.*)/?$ $1/module/status.php?id=$2 [NC,L,QSA]

2
太棒了。目前看来这个方案可行。我会和客户讨论是否这是适合他的正确解决方案。非常好。谢谢Ravinder。 - jexos
1
@jexos,欢迎你,祝你学习愉快。 - RavinderSingh13

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接