地址栏位置的 Apache 重写规则

发布于 2025-01-07 17:42:38 字数 746 浏览 2 评论 0原文

我目前正在尝试制定一个重写规则,该规则将显示 http://mywebsite.com/login/地址栏,但实际上是从 http://mywebsite.com/index.php?action=login 读取的。我已尝试以下操作:

<IfModule mod_rewrite.c>
    Options +FollowSymLinks
    RewriteEngine On
    RewriteBase /
    RewriteRule ^index\.php - [L]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^([a-z0-9\-]+)/?$ /?$.php?page=$1 [L,NC]
</IfModule>

URL 仍然显示为 http://mywebsite.com/index.html。不过,php?action=login

我想,基本上,我想要一个 SEO 风格的链接。

I'm currently trying to make a rewrite rule which will show http://mywebsite.com/login/ in the address bar, but actually read from http://mywebsite.com/index.php?action=login. I've tried the following:

<IfModule mod_rewrite.c>
    Options +FollowSymLinks
    RewriteEngine On
    RewriteBase /
    RewriteRule ^index\.php - [L]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^([a-z0-9\-]+)/?$ /?$.php?page=$1 [L,NC]
</IfModule>

The URL still shows up as http://mywebsite.com/index.php?action=login, though.

Basically, I want a SEO style links, I guess.

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

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

发布评论

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

评论(1

冷月断魂刀 2025-01-14 17:42:38

您可以使用以下内容(静态版本):

Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(index|blogs|projects|contact|login)/?$ index.php?action=$1 [L,NC]

如您所见,每个“页面”都硬编码在 () 之间的规则中。

您还可以使其更加“动态”:

RewriteRule ^([a-z0-9\-]+)/?$ index.php?action=$1 [L,NC]

这将自动使用任何字母(我添加了破折号和数字 - 例如索引、联系人或联系我们)并将其用作“操作”。因此,在您的脚本中,您可以检查其中之一是否不被接受(例如,有人输入:not-a-good-page),然后您可以重定向到 404。这样,您的脚本控制逻辑而不是 .htaccess。

Here's what you can use (static version):

Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(index|blogs|projects|contact|login)/?$ index.php?action=$1 [L,NC]

As you can see, each "page" is hard-coded in the rule between the ().

You can also make it more "dynamic":

RewriteRule ^([a-z0-9\-]+)/?$ index.php?action=$1 [L,NC]

This will automatically use any letters (I added dash and numbers - like index, contact or contact-us) and use it as "action". So in your script you can check if one of these is not accepted (for example someone type: not-a-good-page) then you can redirect to a 404. This way, your script controls the logic not the .htaccess.

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