.htaccess的代码删除从URL中的id和标题? [重复](Htaccess code to

2019-10-30 03:11发布

这个问题已经在这里有一个答案:

  • htaccess的PHP重写view.php?visopslag =(ID) 2个答案

我用我的.htaccess文件中添加www. 索引网址,删除.php和删除?id=从URL中。

这是原始地址:

www.site.com/article.php?id=12&title=title-text

网址的.htaccess代码

www.site.com/article/12

此代码已删除了&title=title-text的URL。

如何删除&title=title-text ? 像这样:

www.site.com/article/12/title-text

.htaccess文件

Options +FollowSymLinks
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} ^ifeelvideos.com [NC]
RewriteRule ^(.*)$ http://www.ifeelvideos.com/$1 [L,R=301]

# To externally redirect /dir/foo.php?id=123 to /dir/foo
RewriteCond %{THE_REQUEST} ^GET\s([^.]+)\.php\?id=([^&\s]+) [NC]
RewriteRule ^ %1/%2? [R,L]

# To internally forward /dir/foo/12 to /dir/foo.php?id=12
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+?)/([^/]+)/?$ $1.php?id=$2 [L,QSA]

# To externally redirect /dir/foo.php to /dir/foo
RewriteCond %{THE_REQUEST} ^GET\s([^.]+)\.php\s [NC]
RewriteRule ^ %1 [R,L]

# To internally forward /dir/foo to /dir/foo.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteCond %{QUERY_STRING} ^$
RewriteRule ^(.*?)/?$ $1.php [L]

#Alternate default index pages
DirectoryIndex first.html index.htm index.html index.php

Answer 1:

我假设article.php实际上并不需要的标题来操作,只有编号。 你可以,如果标题在首位的URL存在标题只添加到URL。 你可以离开了第二个规则,如果title是始终在URL中。

# To externally redirect /dir/foo.php?id=123&title=456 to /dir/123/456
RewriteCond %{THE_REQUEST} ^GET\s([^.]+)\.php\?id=([^&]+)&title=([^&\s]+) [NC]
RewriteRule ^ %1/%2/%3? [R,L]

# To externally redirect /dir/foo.php?id=123 to /dir/123
RewriteCond %{THE_REQUEST} ^GET\s([^.]+)\.php\?id=([^&\s]+)\s [NC]
RewriteRule ^ %1/%2? [R,L]

# To internally forward /dir/foo/12 to /dir/foo.php?id=12
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+?)/([^/]+)(/[^/]+)?/?$ $1.php?id=$2 [L,QSA]

此外,article.php应该检查$_SERVER['REQUEST_URI']如果标题是不是在它执行重定向:

<?php
  $id = intval( $_GET['id'] );
  $title = titleById( $id );

  $parsedurl = explode( "/", $url );
  if( $parsedurl[3] != $title ) {
    header( "Location: http://www.example.com/article/$id/$title", true, 301 );
    exit();
  }


文章来源: Htaccess code to remove id and title from URLs? [duplicate]