简体   繁体   English

如何使用 htaccess 从 URL 中删除 index.php 和索引 | PHP

[英]How to remove index.php and index from the URL using htaccess | PHP

I am working on a PHP website for QA forum.我正在为 QA 论坛开发 PHP 网站。 My website contains a qa folder like -我的网站包含一个 qa 文件夹,例如 -

example.com/qa/ In the qa folder, I have an index.php file. example.com/qa/ 在 qa 文件夹中,我有一个 index.php 文件。 I am getting QA post from datatabase like -我正在从数据库中获取 QA 帖子,例如 -

example/qa/index/506/how-to-connect-the-html-page-to-the-mysql-database-using-php示例/qa/index/506/how-to-connect-the-html-page-to-the-mysql-database-using-php

I am using htaccess code (htaccess located in the same qa folder ) -我正在使用 htaccess 代码(htaccess 位于同一个 qa 文件夹中)-

RewriteEngine on

    RewriteRule ^index/([0-9]+)/([0-9a-zA-Z_-]+)$ index.php?id=$1&slug=$2 [NC,L]

According to this htaccess file, I can open URL like -根据这个 htaccess 文件,我可以像打开 URL 一样 -

example/qa/index/506/how-to-connect-the-html-page-to-the-mysql-database-using-php

not不是

example.com/qa/index.php?id=506&slug=how-to-connect-the-html-page-to-the-mysql-database-using-php

This is working well and I like it but the main thing starts from here.这运作良好,我喜欢它,但主要的事情从这里开始。
I want to remove index from url我想从 url 中删除索引

before

example/qa/index/506/how-to-connect-the-html-page-to-the-mysql-database-using-php

after

example/qa/506/how-to-connect-the-html-page-to-the-mysql-database-using-php

I have used this code in the same htaccess file我在同一个 htaccess 文件中使用了这个代码

RewriteEngine On
RewriteCond %{THE_REQUEST} ^GET.*index\.php [NC]

RewriteRule (.*?)index\.php/*(.*) /$1$2 [R=301,NE,L]

but it's not working.但它不起作用。
One more thing, I will create more pages in qa folder like - login.php, register.php.还有一件事,我将在 qa 文件夹中创建更多页面,例如 - login.php、register.php。

How can I remove the index from the URL without effects on other pages in qa folder.如何从 URL 中删除索引而不影响 qa 文件夹中的其他页面。

How to remove the index from url如何从 url 中删除索引

example.com/qa/index.php?id=506&slug=how-to-connect-the-html-page-to-the-mysql-database-using-php

to

example/qa/index/506/how-to-connect-the-html-page-to-the-mysql-database-using-php

before

Just create a htaccess file in your folder where is index.php located.只需在 index.php 所在的文件夹中创建一个 htaccess 文件。

RewriteEngine on
RewriteRule ^([0-9]+)/([0-9a-zA-Z_-]+)$ index.php?id=$1&slug=$2 [NC,L]

after

example/qa/506/how-to-connect-the-html-page-to-the-mysql-database-using-php 

It will be more valuable for future visitors.对于未来的访客来说,这将是更有价值的。 If you want to remove PHP (.php ) extension from any page inside the folder如果要从文件夹内的任何页面中删除 PHP (.php ) 扩展名

before

example.com/qa/login.php 

after Add code line on htaccess file在 htaccess 文件上添加代码行之后

 RewriteEngine On
    RewriteRule ^login\/?$ login.php

Now, you can open URL like -现在,您可以像打开 URL 一样 -

example.com/qa/login

It's another htaccess method to remove PHP extension.php from the Page URL using htaccess file.这是使用 htaccess 文件从页面 URL 中删除 PHP extension.php 的另一种 htaccess 方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM