从URL中删除index.php和参数名

从URL中删除index.php和参数名,php,apache,.htaccess,Php,Apache,.htaccess,嗨,我正在做一个网站,我想在那里好和干净的链接。我试图使带有get参数的链接看起来更干净。下面是一个示例链接,介绍了该链接目前的情况: http://www.example.com/index.php?item=cd-player 以下是我希望链接的外观: http://www.example.com/cd-player 我已经能够从url中删除index.php,只留下参数,但我还需要帮助删除?项位 以下是我迄今为止所尝试的: RewriteRule ^index\.php(.*)$ /$

嗨,我正在做一个网站,我想在那里好和干净的链接。我试图使带有get参数的链接看起来更干净。下面是一个示例链接,介绍了该链接目前的情况:

http://www.example.com/index.php?item=cd-player
以下是我希望链接的外观:

http://www.example.com/cd-player
我已经能够从url中删除index.php,只留下参数,但我还需要帮助删除?项位

以下是我迄今为止所尝试的:

RewriteRule ^index\.php(.*)$ /$1 [R,L,QSA]
RewriteRule ^(.+?)/?$ /?item=$1 [L,QSA]
此外,这可能是一个php代码,而不是.htaccess文件的代码,但是如果用户输入http://www.example.com/cd-player,它如何知道cd player是名为item的get变量的值

编辑:下面的答案存在的问题是,我使用以下代码删除文件扩展名,下面答案中的代码将文件视为查询:

RewriteRule ^([^\.]+)$ $1.php [NC,L]

你需要这样做。这将允许您拥有干净的链接

RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-d 
RewriteCond %{REQUEST_FILENAME}\.php -f 
RewriteRule ^([^/.]+)$ $1.php [NC,L]

#This means if its not a real file
RewriteCond %{REQUEST_FILENAME} !-f
#This means if its not a real directory
RewriteCond %{REQUEST_FILENAME} !-d
#Then take the capture and rewrite it
RewriteRule ^(.+)$ /index.php?item=$1 [L]
请尝试以下方法:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?item=$1 [L]

实际上,您需要两条规则:

RewriteEngine On

# external redirect from actual URL to pretty one
RewriteCond %{THE_REQUEST} /index\.php\?item=([^\s&]+) [NC]
RewriteRule ^ /%1? [R=302,L,NE]

# add .php extension
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI}\.php -f [NC]
RewriteRule ^(.+?)/?$ /$1.php [L]

# internal forward from pretty URL to actual one
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ /index.php?item=$1 [L,QSA]

我的问题是,同一文件夹中的其他文件现在被视为查询,因为我已从所有文件中删除了文件扩展名。请参见编辑您需要比RewriteRule更强的规则^[^\.]+$$1.php[NC,L]来添加。php扩展名请参见编辑如果您想删除php扩展名,可以添加此规则。