简体   繁体   中英

.htaccess Apache mod rewrite rule always giving a redirect loop

I am normally fine at writing apache redirects but this particular client's server has been giving me problems :c

I am trying to redirect all sub-pages to the main domain but I am always getting a infinite redirection loop.

Here's my .htaccess file - what am I doing wrong?

RewriteEngine on
RewriteCond %{HTTP_HOST} ^example\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.example\.com$
RewriteRule ^test$ "http\:\/\/example\.com\/" [R=301,L]
RewriteCond %{REQUEST_URI} !"^/$"
RewriteRule ^.*$ "http\:\/\/example\.com\/" [R=301,L]

whoa redirect infinity! but say if I do something like this:

RewriteRule ^test$ "http\:\/\/example\.com\/" [R=301,L]

This will redirect http://example.com/test to http://example.com and bam, it works!

Any guidance would be appreciated. Thanks!

// EDIT

The issue is that we have a mailing list plugin which sends the user a confirmation email. After clicking the confirmation button in the confirm email the user is redirected back to our site with a long query string appended to http://example.com/ . The query string is generated so it's never going to be the same.

Here's an example of one of the query strings:

http://example.com/?has_media=false&fan=https%3A%2F%2Fapp.topspin.net%2Fapi%2Fv1%2Ffan%2Fshow%2F63451204%3Fartist_id%3D12053%26auth%3D96419cab29f59a342cde9ca3c0c20b58&campaign=https%3A%2F%2Fapp.topspin.net%2Fapi%2Fv1%2Fartist%2F12053%2Fcampaign%2F10151864

// Edit 2

I tried @Jon Lin's example and the page loads strangely but it does load. Basically, the correct domain loads in some browsers but the assets (CSS, and images do not) and the networks inspector is showing these paths as permanently moved also :s

Try changing your last rule to:

RewriteRule !^(?:index\.(html?|php)|)$ http://example.com/ [R=301,L]

The reason is that after the directory index is mapped (to something like index.php ), the rules get reprocessed again, and thus the extra redirect.

Note that the query string is still going to be at the end. That's because there is no way to match against the query string in a RewriteRule . You need to match against the %{QUERY_STRING} variable. In order to get rid of the query string, you need to add a ? to the end of http://example.com/ .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM