使用 .htaccess 重定向到目录 URL,但显示该 URL 就像在根目录中一样

发布于 2025-01-17 03:24:42 字数 789 浏览 0 评论 0原文

我目前有一个具有以下文件结构的 CRUD:

在此处输入图像描述

index.php 文件重定向到 add-product.phpdelete.php 和其他文件。问题是,URL 看起来像:myapp.com/resources/views/add-product,但我希望它看起来像 myapp.com/add-product。我发现其他 StackOverflow 帖子与此类似,但它重定向到所需的链接,而不是重定向到正确的链接但显示所需的链接,因此该网站无法正常工作(“在此找不到所请求的 URL”)服务器”)。 .htaccess 文件如下所示:

Options +FollowSymLinks +MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
RewriteRule ^resources/(.*)$ /$1 [L,NC,R]

我还使用该文件从 URL 中删除 .php。最后一行是处理错误重定向的行。我该怎么办?感谢您抽出时间。

I currently have a CRUD that has the following file structure:

enter image description here

The index.php file redirects to the add-product.php, delete.php and other files. The problem is, the URL looks like: myapp.com/resources/views/add-product but I want it to look like myapp.com/add-product. I found other StackOverflow posts similar to this, but it redirects to the desired link, instead of redirecting to the correct link but showing the desired one, and because of that the site doesn't work ("The requested URL was not found on this server"). The .htaccess file looks like this:

Options +FollowSymLinks +MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
RewriteRule ^resources/(.*)$ /$1 [L,NC,R]

I'm also using the file to be able to remove the .php from the URLs. The last line is the one handling the wrong redirection. How can I go about this? Thank you for your time.

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

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

发布评论

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

评论(1

红尘作伴 2025-01-24 03:24:42

您可以这样使用它:

Options +FollowSymLinks +MultiViews
RewriteEngine On
RewriteBase /myapp/

# external redirect to remove /resources/views/ from URLs
RewriteRule ^resources/views/(.+)\.php$ $1 [L,NC,R=302]

# internal rewrite to add /resources/views/ and .php to show content
RewriteCond %{DOCUMENT_ROOT}/myapp/resources/views/$1.php -f
RewriteRule ^(.+?)/?$ resources/views/$1.php [END]

一旦确认它工作正常,请将 R=302 替换为 R=301。测试 mod_rewrite 规则时避免使用 R=301(永久重定向)。

You may use it like this:

Options +FollowSymLinks +MultiViews
RewriteEngine On
RewriteBase /myapp/

# external redirect to remove /resources/views/ from URLs
RewriteRule ^resources/views/(.+)\.php$ $1 [L,NC,R=302]

# internal rewrite to add /resources/views/ and .php to show content
RewriteCond %{DOCUMENT_ROOT}/myapp/resources/views/$1.php -f
RewriteRule ^(.+?)/?$ resources/views/$1.php [END]

Once you verify it is working fine, replace R=302 to R=301. Avoid using R=301 (Permanent Redirect) while testing your mod_rewrite rules.

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