[htaccess] Remove query string?
Sorry if I posted in the wrong forum I couldnt really find one, anyway.
I am trying to remove query strings accorss the whole site and replace them with a forward slash and if possible remove the "&" and "=" replace that with with a forward slash, now here is my current rewrite rule to remove .php extension.
I am extremely new to .htaccess and I have 0 knowledge about it, so any help would be appreciated.
Code:
Options +FollowSymLinks
Options -Multiviews
RewriteEngine On
RewriteBase /
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
Re: [htaccess] Remove query string?
I'm not an expert on Apache configs, so don't expect too much from this.
Check the configs for RewriteRule at http://httpd.apache.org/docs/current...ml#rewriterule, specifically the info box with the heading "Modifying the Query String".
Quote:
By default, the query string is passed through unchanged. You can, however, create URLs in the substitution string containing a query string part. Simply use a question mark inside the substitution string to indicate that the following text should be re-injected into the query string. When you want to erase an existing query string, end the substitution string with just a question mark. To combine new and old query strings, use the [QSA] flag.
Re: [htaccess] Remove query string?
Thanks for the reply,
I cant make much out of that sorry, I am pretty new to this sort of thing, not sure how much of this works.
Re: [htaccess] Remove query string?
You only need the "RewriteEngine On" line once.
So... have you tried the htaccess file as you've posted it? If so, is it working as you'd expected, or not? What problems are you having with it? I don't see anything immediately wrong with it, but it depends on what you're trying to achieve.
Re: [htaccess] Remove query string?
Yes it seems to be working, however I have several things I want to remove, like for example non rewritted url would be: url/members.php?pid=1 and I want that to appear like: url/members/2
at the moment its displaying it as: url/members/?pid=2
Thanks for any help.
Re: [htaccess] Remove query string?
htaccess can't discern what part of your "pretty" URL (url/members/2) is a directory path, a file name, or a query string var. So you'd probably need to write some specific rules; for instance:
Code:
RewriteRule ^url/members/([0-9]*)?$ /url/members.php?pid=$1 [NC]
You can create a hierarchy of specific rules followed by a generic one:
Code:
RewriteRule ^url/members/([0-9]*)?$ /url/members.php?pid=$1 [NC,L]
RewriteRule ^url/other/([a-z]*)?$ /url/other.php?var=$1 [NC,L]
RewriteRule ^([^\.]+)$ $1.php [NC,L]
This won't scale very well though: you'll essentially need a new rule for every URL that could have a query string. If your site is small, that may not be too bad.
Re: [htaccess] Remove query string?
Thats perfect theres not many query strings across the whole website, also url bit of the path was the website url.
Thanks!
EDIT: Also to allow alphanumeric in the ?pid= do I just change
Quote:
RewriteRule ^members/([0-9]*)?$ /members.php?pid=$1 [NC]
To
Quote:
RewriteRule ^members/([0-9][a-z]*)?$ /members.php?pid=$1 [NC]
So like it would be like this: www.website.com/members/Username123
Re: [htaccess] Remove query string?
To allow alphanumeric, put the ranges within the same set of square braces:
Code:
RewriteRule ^members/([0-9A-Za-z]*)?$ /members.php?pid=$1 [NC]
(added the uppercase character range too)
Re: [htaccess] Remove query string?
Quote:
Originally Posted by
SambaNeko
To allow alphanumeric, put the ranges within the same set of square braces:
Code:
RewriteRule ^members/([0-9A-Za-z]*)?$ /members.php?pid=$1 [NC]
(added the uppercase character range too)
The [NC] tag means "No Case," so you can specify either the uppercase or lowercase ranges and not have a problem.
Re: [htaccess] Remove query string?
Yeah, I knew what NC stood for, but I always see examples where people just do both ("A-Za-z" and [NC]), and was too lazy to research or test if there was a reason for that. I suppose there isn't.
But why not cover all of your bases in case the official flag reader has the day off? /s
Re: [htaccess] Remove query string?
Thanks guys, will try this once I get back home.
Re: [htaccess] Remove query string?
Sorry guys aint working im getting "No input file specified. ". When typing this: website.com/members/username it redirects to website.com//members.php/username/?pid=username
Also this is the updated .htaccess
Quote:
<files .htaccess>
order allow,deny
deny from all
</files>
Options +FollowSymLinks
Options -Multiviews
RewriteEngine On
RewriteBase /
#Remove .php from urls
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !^admin.*
RewriteCond %{REQUEST_URI} !^/app.*
RewriteCond %{REQUEST_URI} !^/dl.*
RewriteRule ^([^/]+)/$ $1.php
RewriteRule ^([^/]+)/([^/]+)/$ /$1/$2.php
#Remove ?pid= from members url
RewriteRule ^members/([0-9A-Za-z]*)?$ /members.php?pid=$1 [NC]
#Add slash at end of url.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^admin.*
RewriteCond %{REQUEST_URI} !^/app.*
RewriteCond %{REQUEST_URI} !^/dl.*
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/)$
RewriteRule (.*)$ /$1/ [R=301,L]
EDIT: Also another question to add a trailing slash its this right?
Quote:
RewriteRule ^members/([0-9A-Za-z]*)?$/ /members.php?pid=$1 [NC]
Re: [htaccess] Remove query string?
Forgot to mention in a prior post, add the L ("last") flag to have htaccess stop rewriting after a rule:
Code:
#Remove ?pid= from members url
RewriteRule ^members/([0-9A-Za-z]*)?$ /members.php?pid=$1 [NC,L]
And then move this rule above the other two blocks.
To add a slash to the matching pattern, put it before the $:
Code:
RewriteRule ^members/([0-9A-Za-z]*)?/$ /members.php?pid=$1 [NC]
The $ in this pattern signifies the end of the URL. Be aware that this makes the / a requirement for matching the pattern; so "members/username/" would match, but "members/username" would not. If you want to make it optional (so that either would match), add a ?:
Code:
RewriteRule ^members/([0-9A-Za-z]*)?/?$ /members.php?pid=$1 [NC]
Re: [htaccess] Remove query string?
Thank you very much! works like a charm!
Ok one last thing I promise :p
I have a link like this:
Quote:
website.com/members/?page=Stuff&mode=other
Thanks to your help ive got it to:
Quote:
website.com/members/Stuff&mode=other
However the &mode=other doesent seem to work, how would I split it up to something like:
Quote:
website.com/members/Stuff/other/
If not too difficult.
Thanks for everything.
Re: [htaccess] Remove query string?
Never mind I have fixed it.
Sorry for double post cant edit last one.