I tried, unsuccessfully, for hours to build (what I think is) a simple 301 redirect in an htaccess file.

My url looks like this: www.example.com/?my_foo=bar

And I want it to 301 redirect to: www.example.com/foo/bar/

I have read A LOT of guides on this topic and tried dozens of code samples. Nothing worked.

Thanks in advance...

To elaborate, my most recent attempt was :

RewriteCond %{QUERY_STRING} ^my_foo=(.*)$
RewriteRule ^(.*)$ http://www.example.com/foo/$1/? [L,R=301]

The way I see it, I've created a condition, to match the querystring my_foo equals anything.

Then, I've added the redirect: link every page that meets this criteria (meaning, ^(.*)$) to the new address.

I've also added a question mark at the end, to not get recursive function by constantly re-adding the same querystring.

It still doesn't work.

What am I doing wrong?

  • Can you post the rules you have tried? – Gmck Nov 26 '15 at 18:40
  • Sure! re-edited my question @Gmck – Django Nov 26 '15 at 18:59
  • How do you know if your .htaccess file is being used? Is it a virtual host? Can you post your apache virtual host config? – Diamond Nov 26 '15 at 19:43
  • This is one of the few rewrites that I would have the application do, rather than the web server. – Michael Hampton Nov 27 '15 at 8:56
  • Thank you all responders in this thread. After verifying that htaccess is indeed active, as you suggested, turns out part of the problem was my native language not passing through the redirection (NE flag fixed it). Also, $1 does not work so I ended up using %1 (now I read that "percentage sign % brings in the match or matches from the rewriteCond(s) and you use the dollar sign as normal for the matches from the rewriteRule"). Linux environment can be really frustrating sometimes... Thanks again for your much needed help. – Django Nov 27 '15 at 20:27

I suppose your apache configuration is not set to allow the use of .htaccess file. You need to set it either globally or for a particular directory by adding the AllowOverride All option.

.htaccess files provide a way to make configuration changes on a per-directory basis.

To activate it default for DocumentRoot, you add the option to the default vhost file. You can also add the option only for a particular directory in your virtual host config.

For example, in ubuntu with apache 2.2: /etc/apache2/sites-available/default:

<Directory /var/www/>
                Options Indexes FollowSymLinks MultiViews
                AllowOverride All
                Order allow,deny
                allow from all

A must read (your apache2 option may vary, this is for 2.2): https://httpd.apache.org/docs/2.2/howto/htaccess.html.


As you are looking for rewrite cond parameters and not rewrite rule, you need to use %1:

RewriteCond %{QUERY_STRING} ^my_foo=(.*)
RewriteRule .* http://www.example.com/foo/%1/ [L,R=301]

source : https://wiki.apache.org/httpd/RewriteQueryString

Anyway, as said before you need the command AllowOverride All to allow .htaccess

<Directory /var/www/>
     AllowOverride All

source : https://httpd.apache.org/docs/2.4/en/howto/htaccess.html

By the way, if you have access to server configuration, you should use Directory block:

You should avoid using .htaccess files completely if you have access to httpd main server config file. Using .htaccess files slows down your Apache http server. Any directive that you can include in a .htaccess file is better set in a Directory block, as it will have the same effect with better performance.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.