重定向包含查询字符串的所有网址

时间:2015-08-03 10:28:56

标签: apache .htaccess redirect url-redirection http-status-code-301

使用.htaccess我如何将包含查询字符串的所有网址重定向到网址的“规范”版本?

示例:

www.example.com/?bla_bla应重定向到www.example.com /

www.example.com/test/?bla_bla_evil_querystring应重定向到www.example.com/test/

www.example.com/test.html?bla_bla应重定向到www.example.com/test.html

我正在寻找一个网站范围的解决方案,将任何带有查询字符串的网址重定向到相同的网址,而不是查询字符串。

谢谢!

1 个答案:

答案 0 :(得分:1)

最简单的方法是使用mod_rewrite。

import java.util.HashSet;
import java.util.Set;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class CyclicExecutionOfThreads {

    public static void main(String args[]) {

        int totalNumOfThreads = 10;
        PrintJob printJob = new PrintJob(totalNumOfThreads);

        /*
        MyRunnable runnable = new MyRunnable(printJob, 1);
        Thread t1 = new Thread(runnable);

        MyRunnable runnable2 = new MyRunnable(printJob, 2);
        Thread t2 = new Thread(runnable2);

        MyRunnable runnable3 = new MyRunnable(printJob, 3);
        Thread t3 = new Thread(runnable3);

        t1.start();
        t2.start();
        t3.start();
        */

        //OR

        ExecutorService executorService = Executors
                .newFixedThreadPool(totalNumOfThreads);
        Set<Runnable> runnables = new HashSet<Runnable>();

        for (int i = 1; i <= totalNumOfThreads; i++) {
            MyRunnable command = new MyRunnable(printJob, i);
            runnables.add(command);
            executorService.execute(command);
        }

        executorService.shutdown();

    }
}

class MyRunnable implements Runnable {

    PrintJob printJob;
    int threadNum;

    public MyRunnable(PrintJob job, int threadNum) {
        this.printJob = job;
        this.threadNum = threadNum;
    }

    @Override
    public void run() {
        while (true) {
            synchronized (printJob) {
                if (threadNum == printJob.counter) {
                    printJob.printStuff();

                    if (printJob.counter != printJob.totalNumOfThreads) {
                        printJob.counter++;
                    } else {

                        System.out.println();
                        // reset the counter
                        printJob.resetCounter();

                    }

                    printJob.notifyAll();

                } else {
                    try {
                        printJob.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }

        }
    }

}

class PrintJob {
    int counter = 1;
    int totalNumOfThreads;

    PrintJob(int totalNumOfThreads) {
        this.totalNumOfThreads = totalNumOfThreads;
    }

    public void printStuff() {
        System.out.println("Thread " + Thread.currentThread().getName()
                + " is printing");

        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

    }

    public void resetCounter() {
        this.counter = 1;
    }
}

如果查询字符串长度至少为1个字符,则此规则匹配。 RewriteEngine on RewriteCond %{QUERY_STRING} . RewriteRule ^ %{REQUEST_URI} [R,QSD,L] 标志将丢弃查询字符串。 QSD标志将导致重定向。在您测试此规则按预期工作后,请将R标记替换为R以使重定向永久化。

在尚未实现R=301标志的版本上,您可以使用以下代码。请注意,这会在您的网址后面留下QSD,但我无能为力。考虑将Apache升级到最新版本。

?