homepage Welcome to WebmasterWorld Guest from 54.227.160.102
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Pubcon Platinum Sponsor 2014
Home / Forums Index / Code, Content, and Presentation / Apache Web Server
Forum Library, Charter, Moderators: Ocean10000 & incrediBILL & phranque

Apache Web Server Forum

    
Remove parameters in URL with htaccess
jancek



 
Msg#: 4623392 posted 10:46 pm on Nov 14, 2013 (gmt 0)

Hello.

I would like to remove the parameters from http://example.com/ubytovanie-typ/hotel/?typ=hot. I would like to redirect it to http://example.com/ubytovanie-typ/hotel/. Parameters is the prodigy of former redirection. I have tried this solution to remove all params:
RewriteEngine On
RewriteCond %{QUERY_STRING} !="" #if there is a query string
RewriteCond %{QUERY_STRING} !^p=.* #wordpress users: allow post tempalinks
RewriteCond %{REQUEST_URI} !^/wp-admin.* #wordpress users: don't break admin
RewriteRule ^(.*)$ /$1? [R=301,L] #remove query string

But without success. My Wordpress stopped working after that.

Could you please help me with this?

[edited by: phranque at 11:00 pm (utc) on Nov 14, 2013]
[edit reason] Please Use example.com [webmasterworld.com] [/edit]

 

jancek



 
Msg#: 4623392 posted 10:48 pm on Nov 14, 2013 (gmt 0)

URLs are:
http://example.com/ubytovanie-typ/hotel/?typ=hot
and
http://example.com/ubytovanie-typ/hotel/

So I want to remove the parameter typ.

[edited by: phranque at 11:01 pm (utc) on Nov 14, 2013]
[edit reason] exemplified domain [/edit]

penders

WebmasterWorld Senior Member penders us a WebmasterWorld Top Contributor of All Time 5+ Year Member Top Contributors Of The Month



 
Msg#: 4623392 posted 11:39 pm on Nov 14, 2013 (gmt 0)

The code that you have already, specifically:

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


Should already be removing the query string portion from the URL. The problem with WordPress would appear to be another problem.

Note that end of line comments are not supported in later versions of Apache - this will give you an error!

JD_Toims

WebmasterWorld Senior Member Top Contributors Of The Month



 
Msg#: 4623392 posted 11:49 pm on Nov 14, 2013 (gmt 0)

I would [do] personally use %{THE_REQUEST} in similar situations to keep from redirecting internal requests.

RewriteEngine on
RewriteCond %{THE_REQUEST} \?
RewriteCond %{QUERY_STRING} !^p=
RewriteCond %{REQUEST_URI} !^/wp-admin
RewriteRule .? http://www.example.com%{REQUEST_URI}? [R=301,L]

lucy24

WebmasterWorld Senior Member lucy24 us a WebmasterWorld Top Contributor of All Time Top Contributors Of The Month



 
Msg#: 4623392 posted 1:14 am on Nov 15, 2013 (gmt 0)

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

It may be more efficient to say
RewriteCond %{QUERY_STRING} .
But jd's approach is better because it deals with two issues at once.

The rule itself is horrendous overkill. If it applies only to one path-- or to some limited number of paths-- put that into the body of the rule as
RewriteRule ^ubytovanie-typ/hotel/ et cetera
You did say htaccess, right?

At a minimum, constrain the rule to requests in the form
(^|/|\.php)$
replacing .php with any extensions you actually use in public URLS. (Omit entirely if .php is only used for internal rewrites.) The idea is to bypass the rule entirely if it's a request for a non-page file.

Do you have a prefabricated htaccess that came with WordPress? If so you have to be careful to put any new rules in the right places, where they won't conflict with rules required by WordPress.

phranque

WebmasterWorld Administrator phranque us a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month



 
Msg#: 4623392 posted 10:06 am on Nov 15, 2013 (gmt 0)

Parameters is the prodigy of former redirection.

is that a product of spellcheck?
=8)

RewriteRule .? http://www.example.com%{REQUEST_URI}? [R=301,L]

while it wasn't pointed out in JD_Toims' post, you typically want to include the fully qualified protocol and hostname in the RewriteRule Target with a redirect unless you have a good reason not to.

My Wordpress stopped working after that.

there should be more directives in .htaccess for handling WordPress requests.
what do these directives look like?
the order of directives is important.

jancek



 
Msg#: 4623392 posted 12:53 am on Nov 16, 2013 (gmt 0)

So..

The first redirection in my htacess is
RewriteCond %{QUERY_STRING} ^typ=hot$
RewriteRule ^ubytovanie.php$ http://example.com/ubytovanie-typ/hotel/ [R=301,L]


This redirection causes, that the redirected URL looks like this
http://example.com/ubytovanie-typ/hotel/?typ=hot

I want to remove the part ?typ=hot from the target URL.


JD's solution works! Thank you very much.

[edited by: phranque at 10:23 pm (utc) on Nov 16, 2013]
[edit reason] exemplified domain [/edit]

penders

WebmasterWorld Senior Member penders us a WebmasterWorld Top Contributor of All Time 5+ Year Member Top Contributors Of The Month



 
Msg#: 4623392 posted 1:21 am on Nov 16, 2013 (gmt 0)

I want to remove the part ?typ=hot from the target URL.


A trailing ? (question mark) on the RewriteRule substitution effectively specifies an empty query string and removes this from the requested URL.

RewriteRule ^ubytovanie.php$ /ubytovanie-typ/hotel/? [R=301,L]


But I assume you've already solved this with JD's solution? It's also in the code you originally posted.(?)

jancek



 
Msg#: 4623392 posted 1:30 am on Nov 16, 2013 (gmt 0)

Penders, your solution is even more simple. Thank you very much, it works.

So I have replaces JD's solution with penders's. Thanks again.

JD_Toims

WebmasterWorld Senior Member Top Contributors Of The Month



 
Msg#: 4623392 posted 5:21 am on Nov 16, 2013 (gmt 0)

you typically want to include the fully qualified protocol and hostname in the RewriteRule Target with a redirect unless you have a good reason not to.

There's usually a reason why I code mod_rewrite the way I do -- Nearly 10 years of experience is part of it -- Phranque's note/addition cited above is *right on* and I likely should have mentioned it -- There may not be an issue now on the specific box you're on with the specific settings currently in place, but if you ever move [or for some reason your host changes a setting], you could be in for a "head-scratcher" about why your site broke -- I definitely have my "been there, done that" card lol

And:

RewriteRule ^ubytovanie.php$ /ubytovanie-typ/hotel/? [R=301,L]

Also matches: ubytovanie_php ubytovanie-php ubytovanie/php and any other single character that's not the end of a line following ubytovanie and preceding php, so at the very least the rule should likely be the following, unless you want to redirect ubytovanie[any-single-non-line-ending-character]php, which you may, but idk, because what's presented in your original post is totally different than what I'm seeing now.

Anyway, to match exactly ubytovanie.php you need to escape the . and as phranque pointed out, you should always use a fully-qualified URL on the right side for a redirect, unless you know exactly why you don't need/want to.

RewriteRule ^ubytovanie\.php$ http://www.example.com/ubytovanie-typ/hotel/? [R=301,L]

penders

WebmasterWorld Senior Member penders us a WebmasterWorld Top Contributor of All Time 5+ Year Member Top Contributors Of The Month



 
Msg#: 4623392 posted 6:49 pm on Nov 16, 2013 (gmt 0)

Good points. To be honest, my hastily posted copy'n'paste code snippet was intended just to show the location of "a trailing ? on the RewriteRule substitution". I only removed the protocol/domain when the URL was auto-linked in the post, which messed up the output (I should have changed it to example.com - I didn't realise that isn't auto-linked).

you typically want to include the fully qualified protocol and hostname in the RewriteRule Target with a redirect unless you have a good reason not to
----
but if you ever move [or for some reason your host changes a setting], you could be in for a "head-scratcher" about why your site broke


I have seen a number of people mention this, but no specifics, so I am still rather curious as to why? What problems can arise? To be honest, for me, having to specify an absolute path for all redirects would result in less portable, harder to maintain code? For instance, I often test on a development server, which runs on a different subdomain, but use the very same .htaccess file on both (using root-relative URL-paths). So, I often use root-relative URLs because I am moving between servers. What changes can the host make that affects this?

Also, from the Apache docs for RewriteRule [httpd.apache.org]:
If an absolute URL is specified, mod_rewrite checks to see whether the hostname matches the current host. If it does, the scheme and hostname are stripped out and the resulting path is treated as a URL-path.

lucy24

WebmasterWorld Senior Member lucy24 us a WebmasterWorld Top Contributor of All Time Top Contributors Of The Month



 
Msg#: 4623392 posted 8:59 pm on Nov 16, 2013 (gmt 0)

In general it only becomes an issue when a visitor has made two mistakes: asking for the wrong URL (necessitating a redirect) and also asking for the wrong form of the hostname. But you can't put it past a search engine to deliberately ask for the wrong form of everything just to see what will happen.

If you process your htaccess in a text editor that does regular expressions, it's trivial to change

 (/\S+ \[R=30[12].+)
>>
 http://www.example.com$1

and vice versa. Unsupervised global replace. Leading space both times; the Forums here may eat them.

phranque

WebmasterWorld Administrator phranque us a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month



 
Msg#: 4623392 posted 10:22 pm on Nov 16, 2013 (gmt 0)

What problems can arise?


any time your server is configured to accept requests from multiple protocols (http/https) or multiple hostnames (www vs non-www, wildcard subdomains, "alias domains", etc) you will be subject to non-canonical redirects that are independent of the path or query string unless these are specified in the RewriteRule Target.

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / Apache Web Server
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved