1

I have searched all over Stackoverflow and Googled wide and far, but I cannot get this seemingly simple task done.

I'm trying to set a rule in htaccess to clean the URL by removing the query string, but leaving its value intact:

http://example.local/?p=subscribe

Becomes:

http://example.local/subscribe

I have tried these various methods:

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

or,

RewriteCond %{QUERY_STRING} ^p=$ [NC]
RewriteRule ^(/?)?$ $1? [R=301,L,NC]

or,

RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /\?([^\ ]+)
RewriteRule ^$ /%1? [R=301,L]

But nothing works!

Any help will be greatly appreciated.

2
  • Sorry, but it is unclear what you actually ask. What exactly do you mean by "removing the query string". Which is the URL requested in the browser and what request should that URL get rewritten or redirected to? Commented Jan 8, 2019 at 20:17
  • Basically rewrite the url so that the query string "?p=" gets removed. But without removing the last part "subscribe". /?p=subscribe becomes /subscribe Commented Jan 8, 2019 at 21:24

2 Answers 2

2

Have it like this in your .htaccess:

RewriteEngine On

RewriteCond %{THE_REQUEST} \s/([^?]*?)/?\?p=([^\s&]+)\s [NC]
RewriteRule ^ /%1/%2? [R=301,L,NE]

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

8 Comments

Perfect! - Thank you Anubhava, that did the trick. I appreciate your help.
New problem: I updated the .htaccess on the live server, where phpList is in a subdirectory: http://example.com/mailinglist/?p=subscribe Now the rules are no longer working. I've tried adding RewriteBase /mailinglist/ to the rule set, but it did nothing. I also tried removing trailing / dashes, and even typing mailinglist/ where I thought it would work. Help!
The .htaccess file is in the /mailinglist subdirectory. The parent, example.com is a Wordpress installation with it's own, separate htaccess file.
Well, with the updated code, the site first redirects to http://example.com/mailinglist//subscribe (yes, that's two slashes) but immediately after it redirects back to the root http://example.com/mailinglist/.
Yes, the double slash is gone. But somehow all links are redirecting to the root for no apparent reason. I'm not a programmer, but perhaps this behavior is being triggered by something in the php code inside phpList. I should clarify that the redirection happens this way: http://example.com/mailinglist/?p=subscribe -> http://example.com/mailinglist/subscribe -> http://example.com/mailinglist/. Perhaps I should inquire in the phpList discussion group to see if I can get any insights on this anomaly. Thanks Anubhava.
|
0

You can use % to retrieve a rewriteCond Variables

RewriteCond %{QUERY_STRING} ^p=(.*)$ [NC]
RewriteRule ^(.*)?$ %1? [R=301,L,NC]

Will convert any url http:/site.com/url?qs to http:/site.com/qs

4 Comments

Thank you R. Martin, but the result ends up being a '404' error: Typing http://tssl-list.dev/?p=preferences is not found and the url changes to: http://tssl-list.dev/home/alan/Public/tssl-list/preferences
@AlanW: Try: RewriteRule ^$ /%1? [R=301,L]
@anubhava - Thank you, it almost worked. The query string did disappear and the URL is properly formed: http://tssl-list.dev/preferences. However, it still throws a 404 Not Found.
I'm not sure if this is relevant or not, but the site is actually a phpList implementation.

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.