重写规则参数与文件扩展名结合

发布于 2025-02-03 03:25:57 字数 704 浏览 3 评论 0原文

每天都有一个问题,我该如何重写.htaccess。我发现缺少有关.htaccess规则的信息,并且找不到以下答案:

RewriteEngine On
RewriteRule ^user/([^/]*)$ /user.php?username=$1 [L]

现在,此规则确保users.php?username = hello reprites as user.php/hello hello 但是我如何摆脱.php?我是否添加以下条件并在没有.php的情况下更改上一个条件?所以

RewriteEngine On
RewriteRule ^user/([^/]*)$ /user?username=$1 [L]

还是我保留前一个PHP?我发现以下条件可以删除PHP。

    RewriteEngine on
RewriteCond %{THE_REQUEST} /([^.]+)\.php [NC]
RewriteRule ^ /%1 [NC,L,R]


RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^ %{REQUEST_URI}.php [NC,L]

但是,它与user.php不合时宜。

如何重写user.php?用户名= hello to user/hello,同时将.php文件作为index.php的正常文件索引。

There is the daily question again, how can I rewrite my .htaccess. I found there is a lack of information about the .htaccess rules and can not find an answer to the following:

RewriteEngine On
RewriteRule ^user/([^/]*)$ /user.php?username=$1 [L]

Now this rule makes sure that users.php?username=hello rewrites as user.php/hello
but how can I get rid of the .php? Do i add the following condition and change the previous condition without the .php? so

RewriteEngine On
RewriteRule ^user/([^/]*)$ /user?username=$1 [L]

Or do I keep the previous one WITH php? I found the following condition to remove the php.

    RewriteEngine on
RewriteCond %{THE_REQUEST} /([^.]+)\.php [NC]
RewriteRule ^ /%1 [NC,L,R]


RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^ %{REQUEST_URI}.php [NC,L]

however it is NOT working in combination with the user.php.

How can I rewrite user.php?username=hello to user/hello and in the same time have normal .php files as index.php to index.

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文