1

My URL looks something like this:

example.com/index.php?redirected=1

in my .htaccess-file, I want to remove the last part of my url.

I wrote that:

RewriteCond %{HTTP_HOST} ^example.com\/index.php\?redirected=1$ 
RewriteRule ^(.*) http://www.example.com/$1 [L,R=301]

But for some reason, it isn't working. It is right after the line:

RewriteEngine on
RewriteBase /

I found similar questions, but that didn't do the trick. Thanks

With this code in the .htaccess-file, it redirects me to: www.example.com/index.php

RewriteCond %{THE_REQUEST} \ /+(.*)\?redirected=1&?([^\ ]*)
RewriteRule ^ /%1?%2 [L,R]

When i remove the %2 (which i thought was the index.php-part, it'll result in infinte redirects.

Update: I updated the regexp like this:

RewriteCond %{THE_REQUEST} \ /+(.*)\?redirected=1&?([^\ ]*)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?/$1 [L]

still the "index.php" in the end

2
  • What URL do you want your rule to be redirected to? Commented May 18, 2015 at 13:56
  • perfect would be: example.com (so without index.php at the end) Commented May 18, 2015 at 13:59

2 Answers 2

2

Try this instead:

RewriteCond %{THE_REQUEST} \ /+(.*)\?redirect=1&?([^\ ]*)
RewriteRule ^ /%1?%2 [L,R]

The %{HTTP_HOST} variable is only the hostname, it contains no path or query string in it.

Sign up to request clarification or add additional context in comments.

1 Comment

That almost works for me, thanks so far already! Is there a way to remove the index.php as well? - I'll update the question
1

You can use this rule for stripping query string and as well as removing index.php:

RewriteCond %{THE_REQUEST} /index\.php\?redirected=1[&\s] [NC]
RewriteRule ^(.*?)index\.php$ /$1? [L,R=302,NC,NE]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.