Rewrite Url with apache2 - apache

I'm experimenting with CodeIgniter PHP framework, this framework works like:
http://localhost:7777/~dhalsim/ci/index.php/blog
So, I tried to remove index.php part from there. So far I do these:
make $config['index_page'] = "index.php"; to $config['index_page'] = "";
make $config['uri_protocol'] = "REQUEST_URI"; from $config['uri_protocol'] = "AUTO";
enable apache mod_rewrite by "a2enmod rewrite"
put a .htaccess file to /ci directory:
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_URI} ^system.*
RewriteRule ^(.*)$ /index.php/$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]
And of course restart apache server
Here is my apache logs with these configurations:
127.0.0.1 - - [17/Jul/2009:02:21:41 +0300] [localhost/sid#7f48e8ad2968][rid#7f48e8e634c8/initial] (3) [perdir /home/dhalsim/public_html/ci/] strip per-dir prefix: /home/dhalsim/public_html/ci/blog -> blog
127.0.0.1 - - [17/Jul/2009:02:21:41 +0300] [localhost/sid#7f48e8ad2968][rid#7f48e8e634c8/initial] (3) [perdir /home/dhalsim/public_html/ci/] applying pattern '^(.*)$' to uri 'blog'
127.0.0.1 - - [17/Jul/2009:02:21:41 +0300] [localhost/sid#7f48e8ad2968][rid#7f48e8e634c8/initial] (4) [perdir /home/dhalsim/public_html/ci/] RewriteCond: input='/~dhalsim/ci/blog' pattern='^system.*' => not-matched
127.0.0.1 - - [17/Jul/2009:02:21:41 +0300] [localhost/sid#7f48e8ad2968][rid#7f48e8e634c8/initial] (3) [perdir /home/dhalsim/public_html/ci/] strip per-dir prefix: /home/dhalsim/public_html/ci/blog -> blog
127.0.0.1 - - [17/Jul/2009:02:21:41 +0300] [localhost/sid#7f48e8ad2968][rid#7f48e8e634c8/initial] (3) [perdir /home/dhalsim/public_html/ci/] applying pattern '^(.*)$' to uri 'blog'
127.0.0.1 - - [17/Jul/2009:02:21:41 +0300] [localhost/sid#7f48e8ad2968][rid#7f48e8e634c8/initial] (4) [perdir /home/dhalsim/public_html/ci/] RewriteCond: input='/home/dhalsim/public_html/ci/blog' pattern='!-f' => matched
127.0.0.1 - - [17/Jul/2009:02:21:41 +0300] [localhost/sid#7f48e8ad2968][rid#7f48e8e634c8/initial] (4) [perdir /home/dhalsim/public_html/ci/] RewriteCond: input='/home/dhalsim/public_html/ci/blog' pattern='!-d' => matched
127.0.0.1 - - [17/Jul/2009:02:21:41 +0300] [localhost/sid#7f48e8ad2968][rid#7f48e8e634c8/initial] (2) [perdir /home/dhalsim/public_html/ci/] rewrite 'blog' -> 'index.php?/blog'
127.0.0.1 - - [17/Jul/2009:02:21:41 +0300] [localhost/sid#7f48e8ad2968][rid#7f48e8e634c8/initial] (3) split uri=index.php?/blog -> uri=index.php, args=/blog
127.0.0.1 - - [17/Jul/2009:02:21:41 +0300] [localhost/sid#7f48e8ad2968][rid#7f48e8e634c8/initial] (3) [perdir /home/dhalsim/public_html/ci/] add per-dir prefix: index.php -> /home/dhalsim/public_html/ci/index.php
127.0.0.1 - - [17/Jul/2009:02:21:41 +0300] [localhost/sid#7f48e8ad2968][rid#7f48e8e634c8/initial] (2) [perdir /home/dhalsim/public_html/ci/] trying to replace prefix /home/dhalsim/public_html/ci/ with /
127.0.0.1 - - [17/Jul/2009:02:21:41 +0300] [localhost/sid#7f48e8ad2968][rid#7f48e8e634c8/initial] (5) strip matching prefix: /home/dhalsim/public_html/ci/index.php -> index.php
127.0.0.1 - - [17/Jul/2009:02:21:41 +0300] [localhost/sid#7f48e8ad2968][rid#7f48e8e634c8/initial] (4) add subst prefix: index.php -> /index.php
127.0.0.1 - - [17/Jul/2009:02:21:41 +0300] [localhost/sid#7f48e8ad2968][rid#7f48e8e634c8/initial] (1) [perdir /home/dhalsim/public_html/ci/] internal redirect with /index.php [INTERNAL REDIRECT]
Here is the result in Firefox:
404 Not Found:
The requested URL /index.php was not found on this server.
So, what should I do (or where am I wrong) to get work these URLs?
http://localhost:7777/~dhalsim/ci/blog/ instead of http://localhost:7777/~dhalsim/ci/index.php/blog/

Here is how i resolve it:
RewriteEngine on
RewriteCond $1 !^(index\.php|images|flash|css|js|robots\.txt|downloads)
RewriteRule ^(.*)$ /index.php/$1 [L]
Then in the config.php file
/*
|--------------------------------------------------------------------------
| Base Site URL
|--------------------------------------------------------------------------
|
| URL to your CodeIgniter root. Typically this will be your base URL,
| WITH a trailing slash:
|
| http://example.com/
|
*/
$config['base_url'] = "http://localhost:7777/~dhalsim/ci/";
/*
|--------------------------------------------------------------------------
| Index File
|--------------------------------------------------------------------------
|
| Typically this will be your index.php file, unless you've renamed it to
| something else. If you are using mod_rewrite to remove the page set this
| variable so that it is blank.
|
*/
$config['index_page'] = "";
UPDATE
In the routes.php file:
//Point to default controller
$route['default_controller'] = "site/home"; //Set to your controller
// Point to the right controller
$routing_array = array(
'blog' => 'site/blog',
); // set to your blog controler

Use the following Rewrite Rule:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^(.*)$ /index.php/$1 [NC,L]
In order for this to work you should setup your CodeIgniter app on a different port or use /etc/hosts to give it a simple Vhost (127.0.0.1 ci and http://ci:7777/ with Apache set to use the ci vhost.). Then you can set the base url to / and not worry about it. Otherwise you're going to have to deal with confusing directory issues and mod_rewrite.

Related

htaccess canonical URL subdirectory

I have the same problem as htaccess - canonical URL when redirecting to subdirectory, but the solution there appears to use a hardcoded host name in the htaccess file which I can't do.
The following is in my htaccess file in the root directory which works fine for redirecting all requests into the /public directory with the exception of node_modules:
<IfModule mod_rewrite.c>
RewriteEngine on
# Allow node_modules
RewriteRule ^node_modules($|/) - [L]
# Rewrite everything to public
RewriteRule ^(.*)$ public/$1 [L,QSA]
</IfModule>
However I realize that the pages can be accessed through two different URLs, for example:
https://localhost/application1/foo/books.php
https://localhost/application1/public/foo/books.php
How do I either prevent the second one (ideally) or have it redirect to the first one?
I tried various RewriteCond statements with %{THE_REQUEST} but they turned out to be infinite loops. As mentioned above this needs to be hostname-agnostic as the application runs on different environments.
Update
I tried #SuperDuperApps answer below with the following in my .htaccess, which seemed to make no difference:
RewriteEngine on
RewriteCond $1 !^node_modules($|/)
RewriteCond $1 !^public($|/)
RewriteRule ^(.*)$ public/$1 [L,QSA]
RewriteRule ^public/ - [L,R=404]
After enabling RewriteLogLevel 3 in my dev server, this is what appears when I access a file with /public in the URL:
192.168.33.1 - - [27/Jan/2017:22:52:45 --0500] [localhost/sid#7f4a0d1d2cf0][rid#7f4a0d6a5d58/initial] (1) [perdir /var/www/html/application1/public/] pass through /var/www/html/application1/public/common/assets/js/nav.min.js
And this is when I access the same file without /public in the URL (desired behaviour):
192.168.33.1 - - [27/Jan/2017:22:48:45 --0500] [localhost/sid#7f4a0d1d2cf0][rid#7f4a0d684738/initial] (3) [perdir /var/www/html/application1/] add path info postfix: /var/www/html/application1/common -> /var/www/html/application1/common/assets/js/nav.min.js
192.168.33.1 - - [27/Jan/2017:22:48:45 --0500] [localhost/sid#7f4a0d1d2cf0][rid#7f4a0d684738/initial] (3) [perdir /var/www/html/application1/] strip per-dir prefix: /var/www/html/application1/common/assets/js/nav.min.js -> common/assets/js/nav.min.js
192.168.33.1 - - [27/Jan/2017:22:48:45 --0500] [localhost/sid#7f4a0d1d2cf0][rid#7f4a0d684738/initial] (3) [perdir /var/www/html/application1/] applying pattern '^(.*)$' to uri 'common/assets/js/nav.min.js'
192.168.33.1 - - [27/Jan/2017:22:48:45 --0500] [localhost/sid#7f4a0d1d2cf0][rid#7f4a0d684738/initial] (2) [perdir /var/www/html/application1/] rewrite 'common/assets/js/nav.min.js' -> 'public/common/assets/js/nav.min.js'
192.168.33.1 - - [27/Jan/2017:22:48:45 --0500] [localhost/sid#7f4a0d1d2cf0][rid#7f4a0d684738/initial] (3) [perdir /var/www/html/application1/] add per-dir prefix: public/common/assets/js/nav.min.js -> /var/www/html/application1/public/common/assets/js/nav.min.js
192.168.33.1 - - [27/Jan/2017:22:48:45 --0500] [localhost/sid#7f4a0d1d2cf0][rid#7f4a0d684738/initial] (2) [perdir /var/www/html/application1/] strip document_root prefix: /var/www/html/application1/public/common/assets/js/nav.min.js -> /application1/public/common/assets/js/nav.min.js
192.168.33.1 - - [27/Jan/2017:22:48:45 --0500] [localhost/sid#7f4a0d1d2cf0][rid#7f4a0d684738/initial] (1) [perdir /var/www/html/application1/] internal redirect with /application1/public/common/assets/js/nav.min.js [INTERNAL REDIRECT]
192.168.33.1 - - [27/Jan/2017:22:48:45 --0500] [localhost/sid#7f4a0d1d2cf0][rid#7f4a0d676688/initial/redir#1] (1) [perdir /var/www/html/application1/public/] pass through /var/www/html/application1/public/common/assets/js/nav.min.js
This should do it:
RewriteEngine on
# Allow node_modules
RewriteCond $1 !^node_modules($|/)
# Rewrite everything to public except public
RewriteCond $1 !^public($|/)
RewriteRule ^(.*)$ public/$1 [L,QSA]
# 404 diret access to public
RewriteRule ^public/ - [L,R=404]
Got it working with two separate files.
.htaccess:
<IfModule mod_rewrite.c>
RewriteEngine on
# Allow node_modules
RewriteRule ^node_modules($|/) - [L]
# Rewrite everything to public
RewriteRule ^(.*)$ public/$1 [L,QSA]
</IfModule>
.htaccess inside public:
<IfModule mod_rewrite.c>
RewriteEngine on
# pass-through if another rewrite rule has been applied already
RewriteCond %{ENV:REDIRECT_STATUS} 200
RewriteRule .* - [S=1] # Skip the next rule
RewriteRule ^ - [L,R=404]
# ...additional rules here as needed
</IfModule>
[L] can have been used instead of [S=1] in the second file if there are no additional rules.
This question gave me the idea with "pass-through if redirect".
Also thanks to #SuperDuperApps for the debugging hint with RewriteLogLevel and the original answer with the [END] flag that may have worked if I had Apache 2.4.

Can we use RewriteRule for internal paths?

I've always seen RewriteRule used for public URL paths, but can we also map the URLs to internal paths?
For example, to redirect all links to my_page.php, is this allowed? :
RewriteRule .* /home/yccaucom/public_html/my_page.php [last,noescape]
You cannot redirect to a folder or file that is not within the domain root.
Given that your root folder is:
/home/yccaucom/public_html/
You can only redirect to what is within public_html, for example public_html/css or public_html/some_folder, you can also make a symbolic link to an internal folder or file and it should work as well.
Given your rule, it would look like this:
RewriteCond %{REQUEST_URI} !^/my_page.php$ [NC]
RewriteRule ^ /my_page.php [L,NE]
You want the condition to avoid it from falling into a infinite loop.
Or you could check for existent files/folders instead with this 2 conditions:
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
Basically it says if file or folder does not exist redirect.
If you try to use your rule as it is, it will try to redirect to:
/home/yccaucom/public_html/home/yccaucom/public_html/my_page.php
However it will most likely not fail since you don't have any verification to stop the loop so it will give you a fail message saying too many redirects or so.
On a deeper look at the rule, this is what happens:
10.0.0.1 - - [24/Aug/2013:05:25:24 --0300] [somedomain.com/sid#7fe8521040e0][rid#7fe85288af18/initial] (3) [perdir /home/account/public_html/] strip per-dir prefix: /home/account/public_html/asdasdas2 -> asdasdas2
10.0.0.1 - - [24/Aug/2013:05:25:24 --0300] [somedomain.com/sid#7fe8521040e0][rid#7fe85288af18/initial] (3) [perdir /home/account/public_html/] applying pattern '.*' to uri 'asdasdas2'
10.0.0.1 - - [24/Aug/2013:05:25:24 --0300] [somedomain.com/sid#7fe8521040e0][rid#7fe85288af18/initial] (2) [perdir /home/account/public_html/] rewrite 'asdasdas2' -> '/home/account/public_html/edit.php'
10.0.0.1 - - [24/Aug/2013:05:25:24 --0300] [somedomain.com/sid#7fe8521040e0][rid#7fe85288af18/initial] (2) [perdir /home/account/public_html/] trying to replace prefix /home/account/public_html/ with /
10.0.0.1 - - [24/Aug/2013:05:25:24 --0300] [somedomain.com/sid#7fe8521040e0][rid#7fe85288af18/initial] (5) strip matching prefix: /home/account/public_html/edit.php -> edit.php
10.0.0.1 - - [24/Aug/2013:05:25:24 --0300] [somedomain.com/sid#7fe8521040e0][rid#7fe85288af18/initial] (4) add subst prefix: edit.php -> /edit.php
10.0.0.1 - - [24/Aug/2013:05:25:24 --0300] [somedomain.com/sid#7fe8521040e0][rid#7fe85288af18/initial] (1) [perdir /home/account/public_html/] internal redirect with /edit.php [INTERNAL REDIRECT]
10.0.0.1 - - [24/Aug/2013:05:25:24 --0300] [somedomain.com/sid#7fe8521040e0][rid#7fe852858798/initial/redir#1] (3) [perdir /home/account/public_html/] strip per-dir prefix: /home/account/public_html/edit.php -> edit.php
10.0.0.1 - - [24/Aug/2013:05:25:24 --0300] [somedomain.com/sid#7fe8521040e0][rid#7fe852858798/initial/redir#1] (3) [perdir /home/account/public_html/] applying pattern '.*' to uri 'edit.php'
10.0.0.1 - - [24/Aug/2013:05:25:24 --0300] [somedomain.com/sid#7fe8521040e0][rid#7fe852858798/initial/redir#1] (2) [perdir /home/account/public_html/] rewrite 'edit.php' -> '/home/account/public_html/edit.php'
10.0.0.1 - - [24/Aug/2013:05:25:24 --0300] [somedomain.com/sid#7fe8521040e0][rid#7fe852858798/initial/redir#1] (1) [perdir /home/account/public_html/] initial URL equal rewritten URL: /home/account/public_html/edit.php [IGNORING REWRITE]

apache redirect - condition not matched correctly

I have the following simple rewrite-statement and conditions in my virtualhost section of Apache (2.2) config
RewriteCond %{REQUEST_URI} !-d
RewriteCond %{REQUEST_URI} !-f
RewriteRule ^([^\.]{2,})$ /bootstrap.php?url=$1
All requests are nicely redirected to the bootstrap.php, execpt if the file exists. That works fine. However, if a directory exists, I'd expect Apache to run the DirectoryIndex for that directory, or show the contents of the directory (whatever setting I choose), but Apache directs the request to the bootstrap.php too. It seems that the first rewriteCond is somehow not correct. What am I doing wrong?
When doing a debug, and trying to load "localhost/img" (which exists) I get the following output
127.0.0.1 - - [14/Jan/2013:20:18:14 +0100] [localhost/sid#4cd5a8][rid#14442c0/initial] (2) init rewrite engine with requested uri /img/
127.0.0.1 - - [14/Jan/2013:20:18:14 +0100] [localhost/sid#4cd5a8][rid#14442c0/initial] (3) applying pattern '^([^\.]{2,})$' to uri '/img/'
127.0.0.1 - - [14/Jan/2013:20:18:14 +0100] [localhost/sid#4cd5a8][rid#14442c0/initial] (4) RewriteCond: input='/img/' pattern='!-d' => matched
127.0.0.1 - - [14/Jan/2013:20:18:14 +0100] [localhost/sid#4cd5a8][rid#14442c0/initial] (4) RewriteCond: input='/img/' pattern='!-f' => matched
127.0.0.1 - - [14/Jan/2013:20:18:14 +0100] [localhost/sid#4cd5a8][rid#14442c0/initial] (2) rewrite '/img/' -> '/bootstrap.php?url=/img/'
127.0.0.1 - - [14/Jan/2013:20:18:14 +0100] [localhost/sid#4cd5a8][rid#14442c0/initial] (3) split uri=/bootstrap.php?url=/img/ -> uri=/bootstrap.php, args=url=/img/
127.0.0.1 - - [14/Jan/2013:20:18:14 +0100] [localhost/sid#4cd5a8][rid#14442c0/initial] (2) local path result: /bootstrap.php
127.0.0.1 - - [14/Jan/2013:20:18:14 +0100] [localhost/sid#4cd5a8][rid#14442c0/initial] (2) prefixed with document_root to C:/webroot/zippyshoot/bootstrap.php
127.0.0.1 - - [14/Jan/2013:20:18:14 +0100] [localhost/sid#4cd5a8][rid#14442c0/initial] (1) go-ahead with C:/webroot/zippyshoot/bootstrap.php [OK]
Works fine when loading localhost/img/logo.png. Output of the debug log is then
127.0.0.1 - - [14/Jan/2013:20:27:56 +0100] [localhost/sid#4cd5a8][rid#237d978/initial] (2) init rewrite engine with requested uri /img/logo.png
127.0.0.1 - - [14/Jan/2013:20:27:56 +0100] [localhost/sid#4cd5a8][rid#237d978/initial] (3) applying pattern '^([^\.]{2,})$' to uri '/img/logo.png'
127.0.0.1 - - [14/Jan/2013:20:27:56 +0100] [localhost/sid#4cd5a8][rid#237d978/initial] (1) pass through /img/logo.png
what am I doing wrong?
I found the answer to the question in this post
http://amandine.aupetit.info/135/apache2-mod_rewrite/
As from Apache 2.2, the rewriteCond is changed. The correct way to write them is now
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_FILENAME} !-f
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_FILENAME} !-l

Mod Rewrite is giving me a hard time

I'm currently using wamp on windows 7. I'd like to clean my urls obviously. I've tried to find the current syntax and what not, but I haven't figured it out.
My path right now is localhost/rs/index.php
When I go to localhost/rs/user it gives me a 404, but localhost/rs/ gives me the index.php page.
This is what I have in my .htaccess file at the www directory of wamp.
RewriteEngine On
RewriteRule ^/$ index.php
RewriteRule ^/([a-z]+)$ index.php?page=$1
RewriteRule ^/([a-z]+)/$ index.php?page=$1
I have un-commented the line
LoadModule rewrite_module modules/mod_rewrite.so
in the httpd.conf file of Apache
What's wrong? Is my .htaccess file in the wrong spot? Is my syntax wrong?
Thanks!
Place your .htaccess in /rs folder and try
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([A-Za-z0-9]*)/?$ index.php?page=$1 [L]
Hope this will help
Your .htaccess file should be placed in '/rs' folder, in the same directory where index.php is.
I've tried with enabled rewrite log, and what I saw there, when tried to access localhost/test/user:
::1 - - [12/Oct/2012:09:53:11 +0400] [localhost/sid#68a898][rid#1cc4d48/initial] (3) [perdir D:/Development/htdocs/test/] strip per-dir prefix: D:/Development/htdocs/test/user -> user
::1 - - [12/Oct/2012:09:53:11 +0400] [localhost/sid#68a898][rid#1cc4d48/initial] (3) [perdir D:/Development/htdocs/test/] applying pattern '^/$' to uri 'user'
::1 - - [12/Oct/2012:09:53:11 +0400] [localhost/sid#68a898][rid#1cc4d48/initial] (3) [perdir D:/Development/htdocs/test/] strip per-dir prefix: D:/Development/htdocs/test/user -> user
::1 - - [12/Oct/2012:09:53:11 +0400] [localhost/sid#68a898][rid#1cc4d48/initial] (3) [perdir D:/Development/htdocs/test/] applying pattern '^/([a-z]+)$' to uri 'user'
::1 - - [12/Oct/2012:09:53:11 +0400] [localhost/sid#68a898][rid#1cc4d48/initial] (3) [perdir D:/Development/htdocs/test/] strip per-dir prefix: D:/Development/htdocs/test/user -> user
::1 - - [12/Oct/2012:09:53:11 +0400] [localhost/sid#68a898][rid#1cc4d48/initial] (3) [perdir D:/Development/htdocs/test/] applying pattern '^/([a-z]+)/$' to uri 'user'
::1 - - [12/Oct/2012:09:53:11 +0400] [localhost/sid#68a898][rid#1cc4d48/initial] (1) [perdir D:/Development/htdocs/test/] pass through D:/Development/htdocs/test/user
From the first line it is clear, that mod_rewrite is stripping beginning '/', and you are getting 'user' instead of '/user'. So, rewrite rules should be written without '/', that is:
RewriteEngine On
RewriteRule ^$ index.php
RewriteRule ^([a-z]+)/?$ index.php?page=$1
Also notice, that I've combined two last rules by writing '/?'. That means '/' symbol at the end of url is optional.
In order to turn on rewrite log, set the following in your httd.conf file:
#
# Logging for mod_rewrite
# Use RewriteLogLevel 3 only for debug purposes
# Normally use RewriteLogLevel 0
#
<IfModule rewrite_module>
RewriteLogLevel 3
RewriteLog "logs/rewrite.log"
</IfModule>
That way log will be created in logs/rewrite.log file. And this is usually the best way to examine what goes wrong.
If you're running it in subfolder you need to add this line after RewriteEngine On
RewriteBase /rs
Also, make sure that in your apache virtual host section has this value
AllowOverride All

.htaccess ignore query_string adword

I'm trying to ignore a google adword get that is coming through.
Url coming in as:
/location/&gclid=287ejek22kj
This is going to a 404 page because of the gclid...
I need it to go to:
/location
I've tried this, with no success:
RewriteEngine On
RewriteBase /
RewriteRule ^(.*)&gclid=(.*)$ $1 [L]
All help is appreciated. Thanks.
Works here:
192.168.1.2 - - [01/May/2011:05:38:48 +0100] [192.168.1.2/sid#938b98][rid#2789a40/initial] (3) [perdir C:/HTTP/htdocs/] strip per-dir prefix: C:/HTTP/htdocs/location/&gclid=287ejek22kj -> location/&gclid=287ejek22kj
192.168.1.2 - - [01/May/2011:05:38:48 +0100] [192.168.1.2/sid#938b98][rid#2789a40/initial] (3) [perdir C:/HTTP/htdocs/] applying pattern '^(.*)&gclid=(.*)$' to uri 'location/&gclid=287ejek22kj'
192.168.1.2 - - [01/May/2011:05:38:48 +0100] [192.168.1.2/sid#938b98][rid#2789a40/initial] (2) [perdir C:/HTTP/htdocs/] rewrite 'location/&gclid=287ejek22kj' -> 'location/'
Check your rewrite log. See RewriteLog and RewriteLogLevel.
Looks alright but you can also try this:
Options +FollowSymlinks -MultiViews
RewriteEngine on
RewriteRule ^([^&]+)&gclid=.*$ /$1 [L,NC,R=301]