从URL末尾删除?tmpl = component& print = 1

时间:2016-02-05 10:38:13

标签: regex .htaccess

我试图重定向:

www.domain.com/someword/xxxx-other-words

要:

.htaccess

这是我在RewriteCond %{QUERY_STRING} "?tmpl=component&print=1" [NC] RewriteRule (.*) /$1? [R=301,L] 中使用的代码:

500 IS Error

返回String html = "" +"<p>" +" <a href=\"http://www.today.com/video/jill-martin-rescues-savannah-guthrie-from-her-guest-room-mess-604921923959\" rel=\"nofollow\"> Jill Martin rescues Savannah Guthrie from her guest room mess </a> " +" <a href=\"http://www.today.com/video/4-simple-ways-to-clear-your-clutter-this-year-596741699678\" rel=\"nofollow\"> 4 simple ways to clear your clutter this year </a> " +" <a href=\"http://www.today.com/video/staying-home-on-new-years-eve-great-ideas-to-celebrate-at-home-594027587814\" rel=\"nofollow\"> Staying home on New Year's Eve? Great ideas to celebrate at home </a> " +" <a href=\"http://www.today.com/video/heres-how-to-set-a-functional-christmas-table-591622211749\" rel=\"nofollow\"> Here's how to set a functional Christmas table </a> " +"</p>"; Document doc = Jsoup.parse(html); String tag = null; for (Element element : doc.select("*") ) { tag = element.tagName(); if ( "a".equalsIgnoreCase( tag ) ) { System.out.println("element : "+element.ownText()+"; nextElementSibling: "+element.nextElementSibling()+"" ); } if ( StringUtils.containsIgnoreCase(element.ownText(), "Jill Martin rescues Savannah") ) { System.out.println("element : "+element.ownText()+"; nextElementSibling: "+element.nextElementSibling()+"" ); System.out.println("tag : "+tag+"; nextNodeSibling: "+element.nextSibling()+"" ); System.out.println("element : "+element.ownText()+"; previousElementSibling: "+element.previousElementSibling()+"" ); } }

2 个答案:

答案 0 :(得分:0)

如果你有apache,请尝试使用重写规则。

RewriteEngine on
RewriteRule (.*)(\?tmpl=component&print=1) $1 [R=301,L]

<强> Regex101 Demo for replacement only.

答案 1 :(得分:0)

您似乎忘记启用mod_rewrite(可以在/var/log/apache2/error.log中查看)或AllowOverride指令不存在。

我做了(在新鲜的debian / jessie上):

# apt-get install apache2
# a2enmod rewrite
  add AllowOverride into /etc/apache2/sites-enabled/000-default.conf
# systemctl restart apache2

您的网站配置中应该有AllowOverride All&lt; Directory&gt;,类似

<VirtualHost *:80>
        ServerAdmin webmaster@localhost
        DocumentRoot /var/www/html

        <Directory "/var/www/html">
                AllowOverride All
        </Directory>

        ErrorLog ${APACHE_LOG_DIR}/error.log
        CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

和/var/www/html/.htaccess(默认网站)

RewriteEngine on
RewriteCond %{QUERY_STRING} tmpl=component&print=1 [NC]
RewriteRule (.*) /$1? [R=301,L]

这适合我。