地址栏位置的 Apache 重写规则
我目前正在尝试制定一个重写规则,该规则将显示 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用以下内容(静态版本):
如您所见,每个“页面”都硬编码在
()
之间的规则中。您还可以使其更加“动态”:
这将自动使用任何字母(我添加了破折号和数字 - 例如索引、联系人或联系我们)并将其用作“操作”。因此,在您的脚本中,您可以检查其中之一是否不被接受(例如,有人输入:not-a-good-page),然后您可以重定向到 404。这样,您的脚本控制逻辑而不是 .htaccess。
Here's what you can use (static version):
As you can see, each "page" is hard-coded in the rule between the
()
.You can also make it more "dynamic":
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.