如何在htaccess中创建永久链接

Emr*_*mre 3 .htaccess permalinks

我想在Linux主机中使用.htaccess文件将链接重定向到另一个链接.你能帮助我吗?

from: http://example.com/examp
to: http://example.com/examp.php
Run Code Online (Sandbox Code Playgroud)

另一个是我的其他网站

from: http://example.com/examp
to: http://example.com/user.php?u=examp
Run Code Online (Sandbox Code Playgroud)

Laz*_*One 5

您需要为此启用mod_rewrite.首先将这些行放入.htaccess:

RewriteEngine On
RewriteBase /
Run Code Online (Sandbox Code Playgroud)

TBH我不是100%确定你的确切意思是永久链接以及你想如何重定向,所以我将为每个URL提供2个变体:重写(内部重定向)和重定向(301永久重定向).

1.这将重写为(内部重定向)请求http://example.com/examphttp://example.com/examp.php而URL将保持不变在浏览器中:

RewriteRule ^examp$ examp.php [L]
Run Code Online (Sandbox Code Playgroud)

2.当浏览器中的URL发生变化时,这将与上面相同,但具有适当的重定向(301永久重定向):

RewriteRule ^examp$ http://example.com/examp.php [R=301,L]
Run Code Online (Sandbox Code Playgroud)

3.这将重写为(内部重定向)请求http://example.com/examphttp://example.com/user.php?u=examp而URL将保持不变在浏览器中:

RewriteRule ^examp$ user.php?u=examp [QSA,L]
Run Code Online (Sandbox Code Playgroud)

4.当浏览器中的URL发生变化时,这将与上面相同但具有适当的重定向(301永久重定向):

RewriteRule ^examp$ http://example.com/user.php?u=examp [QSA,R=301,L]
Run Code Online (Sandbox Code Playgroud)

有用的链接:http://httpd.apache.org/docs/current/rewrite/