1

I have this in my .htaccess file:

RewriteEngine On
RewriteCond %{THE_REQUEST} /index\.php\?p=(?!admin)(?!superadmin)((?![^&]*?edit)[^\s&]+) [NC]
RewriteCond %{THE_REQUEST} /index\.php\?p=([^\s&]+) [NC]
RewriteRule ^ /%1? [R=301,L,QSA]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/?$ index.php?p=$1 [L,QSA]

This rewrites in both directions. One way from:

domain.com/index.php?p=Home&l=en&m=0

to:

domain.com/Home

and vice versa. What I would like to do is preserve the query string part of the URL that comes after the value of 'p' and pass it along BOTH WAYS.

I was able to do one way, with this code:

RewriteEngine On
RewriteCond %{THE_REQUEST} /index\.php\?p=(?!admin)(?!superadmin)((?![^&]*?edit)[^\s&]+) [NC]
RewriteCond %{THE_REQUEST} /index\.php\?p=([^\s&]+)&p=([^\s&]+) [NC]
RewriteRule ^ /%1?%2? [R=301,L,QSA]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/?$ index.php?p=$1 [L,QSA]

but I can't get the other way right. Can you guys help? I basically want the value of p to be written as part of the URL after the domain and I don't want it to reappear again but I want all the other variables to be appended.

Right now what I get is:

domain.com/Home?m=0?&p=Home&ms=m0&l=en
2
  • What is pretty URL for domain.com/Home?m=0?&p=Home&ms=m0&l=en ? Commented Jul 24, 2014 at 7:35
  • @anubhava that is pretty url. ugly url would be domain.com/index.php?p=Home&ms=m0&l=en but I am trying to make the pretty url be domain.com/Home?ms=m0&l=en Commented Jul 24, 2014 at 14:58

1 Answer 1

1

This should work for you:

RewriteEngine On

RewriteCond %{THE_REQUEST} /index\.php\?p=((?!(?:admin|superadmin|[^&]*?edit))[^\s&]+)(?:&(\S+))? [NC]
RewriteRule ^ /%1?%2 [R=302,L,NE]

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/?$ index.php?p=$1 [L,QSA]
Sign up to request clarification or add additional context in comments.

5 Comments

thanks for your suggestion but that yields the same result, which is that the value of "p" is used twice, first as replacement value for pretty url and then as query string
That is not correct. This redirects /index.php?p=Home&ms=m0&l=e to /Home?ms=m0&l=e as you wrote. Most likely your browser has cached results. Test in a new browser or clear cache.
you are CORRECT and I owe you an apology. I stand corrected. When I cleared all the caches I saw the behavior I was hoping to see. Thank you!
I got one final issue with this... the redirect breaks (404) when I have p with a slash in it (like: p=Folder/File) - when there is a slash in the value of "p" domain.com/index.php?p=Folder/File&ms=0 rewrites to domain.com/Folder/File?ms=0 (which is correct) but I get a 404 error even though without the htaccess file the file is found - would really appreciate your help! thanks
Change last rule to: RewriteRule ^(.+?)/?$ index.php?p=$1 [L,QSA]

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.