在浏览器中显示Pretty URL

时间:2016-02-26 06:23:45

标签: php .htaccess mod-rewrite url-rewriting friendly-url

我可以制作以下网址:

  • example.com/video1

重定向到:

  • example.com/video/video1.php

但我希望它显示:

  • example.com/video1

在浏览器中,但事实并非如此。

显示:

  • example.com/video/video1.php

当我转到网址时:

  • example.com/video/video1.php

应该显示

  • example.com/video1

在浏览器中但显示:

  • example.com/video/video1.php

我该怎么做?

以下是我正在使用的内容:

RewriteEngine On
RewriteRule ^video1 /videos/video1.php [QSA,NC,L]
RewriteRule ^/videos/video1.php /video1  [R=301,L]

3 个答案:

答案 0 :(得分:1)

您可以在 /root/.htaccess

中使用以下规则
RewriteEngine on
#1--Redirect from "/videos/foo.php" to "/foo"--#
RewriteCond %{THE_REQUEST} /videos/([^.]+)\.php [NC]
RewriteRule ^ /%1 [NE,NC,L,R]
#2--Internally map "/foo" to "/videos/foo.php--#
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/?$ /videos/$1.php [NC,L]

在测试之前清除浏览器的缓存。

答案 1 :(得分:0)

你试过吗

Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^yoursite.com
RewriteRule (.*) http://www.yoursite.com/$1 [R=301,L]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9} /index.html HTTP/
RewriteRule ^index.html$ http://www.yoursite.com/ [R=301,L]

来源:

  

https://www.branded3.com/blog/htaccess-mod_rewrite-ultimate-guide/

答案 2 :(得分:0)

您忘记在正则表达式中添加结束$。 请试试这个:

RewriteEngine On
RewriteRule ^/video1$ /videos/video1.php [QSA,NC,L]
RewriteRule ^/videos/video1.php$ /video1  [R=301,L]

而不是

RewriteEngine On
RewriteRule ^video1 /videos/video1.php [QSA,NC,L]
RewriteRule ^/videos/video1.php /video1  [R=301,L]