Modify query string in .htaccess - .htaccess

I am currently updating our website and want to redirect from old url's to the new ones. I appliled several rewriterules to get nice urls without query strings for the new pages, this works perfectly. But i ran into problems when trying to do the following:
I want to change an url's query string from
www.domain.com/?content=foo
to
www.domain.com/index.php?content=bar
or even better, rewrite directly to the new , nice url i have already generated by rewrite rules:
to
www.domain.com/niceurl
in the .htaccess file using a RewriteRule.
I tried this:
RewriteCond %{QUERY_STRING} content=foo
RewriteRule content=bar$ content=foo [L]
or
RewriteCond %{QUERY_STRING} content=foo
RewriteRule niceurl$ content=foo [L]
and a lot of other variations, i just can't get it to work. Any ideas?

If you want to redirect, i.e. tell the client to forget about the old URL and fetch the new shiny URL instead, you must look for the old one (RewriteRule pattern / and RewriteCond QUERY_STRING) and then redirect R to the new URL
RewriteCond %{QUERY_STRING} content=foo
RewriteRule ^$ /niceurl [R,L]

Related

Htaccess 301 redirect with query string params

This is so elementary that you have to ask why it is so complex to implement.
Simply need to redirect one url to another on the same domain, the url contains query string aparams though:
I tried:
RewriteRule ^article.php?section=exclusive&id=543 articles/view/4639 [R=301,L]
I get page cannot be found - the redirect is not happening. I have other re-directs in the same htaccess which work. The query string params are causing problems, and escaping them also does not help.
Note the id's of the two urls are not the same and therefore I don't want a clever re-write rule which converts from one url to another with the query strings mapped to new params. I just want a fast and dirty mapping from A to B.
UPDATE
I tried this it redirects but it is adding my old query string to the end of the new url. How can I prevent that?
RewriteCond %{REQUEST_URI} ^/article\.php$
RewriteCond %{QUERY_STRING} ^section=exclusive
RewriteRule ^$ http://www.onlinegooner.com/articles/view/3000 [R=301,L]
Input url is: mydomain.com/article.php?section=exclusive
You may use this rule:
RewriteCond %{QUERY_STRING} ^section=exclusive [NC]
RewriteRule ^article\.php$ /articles/view/3000? [R=301,L,NC]
? in the target will strip off previous query string.

How to rewrite url for certain pages in htaccess?

I am trying to show different url and redirect users to specific url with .htcaccess when they click on a blog post but to no avail.
Lets say the url is: http://localhost/mySite/article.php?article_title=test-title
then I would like to show it as http://localhost/mySite/article/test-title
This is my current htcaccess file:
#turn on url rewriting
RewriteEngine on
#remove the need for .php extention
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
#rewrite rule for blog
RewriteRule article/([A-Za-z0-9-]+) /mySite/article.php?article_title=$1
But for some reason it is not redirecting/showing the correct url. I am not getting any errors.
EDIT
Trying to ask my question again and explain it better. Let's say the url is
http://localhost/www.example.com/admin/editUser.php?user_id=126
and I would like to rewrite the url like this:
http://localhost/www.example.com/admin/user/126
then how can I achieve this. I tried using this website to check the modified url but it does not work. Seems like it does not work with any of the accepted answers here in stack at all.
This is my htaccess file atm. It is in the root of www.example.com
#turn on url rewriting
Options +FollowSymlinks
RewriteEngine on
RewriteRule ^user/([0-9]+)/?$ /editUser.php?user_id=$1 [NC,L] # Handle user edit requests
Apache Module mod_rewrite is enabled. Also added an alias. Still no changes in the url. If I try something really basic like this:
# redirect to .php-less link if requested directly
RewriteCond %{THE_REQUEST} ^[A-Z]+\s.+\.php\sHTTP/.+
RewriteRule ^(.+)\.php $1 [R=301,L]
it works fine.
Why is the users redirect not working? What am I doing wrong.
Try it like this for your rule for article url in mysite directory.
RewriteRule ^article/([A-Za-z0-9-]+)$ article.php?article_title=$1 [QSA,NC,L]
you need to mention start ^ and end $ of string.

Rewriting old sites urls to new site's urls

The old site's urls are like:
example.com/dsk/newslong.php?id=5206
New site's urls are like:
example.com/dsk/?p=5206
The .htaccess below is not working in any way.
And more, i have 5K posts to map, and would like to have it working like a macro:
(every number after http://www.example.com/dsk/newslong.php?id=XXX should be rewrited to /dsk/?p=XXX)
Options +FollowSymlinks
RewriteEngine on
RewriteRule ^/dsk/newslong.php?id=5206(.*)$ http://www.example.biz/dsk/?p=5206$1 [r=301,nc]
The server once installed newsite is returning a 404 not found on newslong.php.
RewriteEngine on
RewriteCond %{REQUEST_URI} ^/dsk/newslong.php$
RewriteCond %{QUERY_STRING} ^(id=[0-9]+)$
RewriteRule .* /dsk/?%1 [L,QSA]
RewriteRule is matching URI only, you can't attempt matching query string there.
But you can match it within RewriteCond condition...
That being said, if you don't care about query string content, but want all the urls that pointed to newslong.php redirected to a new location, then it's much simpler:
RewriteEngine on
RewriteCond %{QUERY_STRING} ^id=[0-9]+$
RewriteRule ^dsk/newslong\.php$ /dsk/ [L,R]
If You omit the R, then the browser won't be redirected (the user will keep seeing old url) and instead the content will be served via sub-request.

redirect old query URL to new rewritten URL .htaccess

I've successfully made my urls clean using parameters
from example.com/index.php?gender=m&height=70&weight=150
into example.com/m/70/150/
so if someone types in that clean URL, they'll get the page with the proper parameters.
However, I want to make it so if someone types in the old url, it will 301 redirect to the new clean URL
I've tried this
RewriteCond %{QUERY_STRING} ^gender=([a-z]+)&height=([0-9-]+)&weight=([0-9-]+)$
RewriteRule ^index\.php$ http://www.example.com/%1/%2/%3? [R=301,L]
but it is not working, no error, no rewrite.. the URL just looks like the old one still.
Edit:
Current rewrite to make URL's look clean
RewriteEngine on
RewriteRule ^([m]+)en/([0-9-]+)-inches/([0-9-]+)-lbs/?$ index.php?gender=$1&height=$2&weight=$3 [NC,L]
You should probably be checking against the actual request as opposed to the URI/query-string because both of those get modified by other rules. There's also something you're missing in the redirect, the en, -inches, and -lbs in part of the cleaner looking URI.
Try:
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.php\?gender=([a-z]+)&height=([0-9-]+)&weight=([0-9-]+)
RewriteRule ^index\.php$ /%1en/%2-inches/%3-lbs/? [L,R=301]

htaccess mod_rewrite with dynamic parameters

I am new to mod_rewrite. I am trying to forward a URL to another one, but I cannot get it to work.
Say I want to forward this URL:
/cansas.php?m=2&id=2-0-0-0&sid=cansas to
/cansas-is-good-for-you and let the header respond with a 301, or just update the URL through [R].
I have this in my .htaccess:
Options +FollowSymlinks
RewriteEngine on
RewriteRule ^cansas.php?m=2&id=2-0-0-0&sid=cansas$ cansas-is-good-for-you [NC,R=301]
I figured I could just do a simple forwarding, but somewhere along the way it stops working. If I cut out the ?m=2&id= etc, it forwards just the cansas part to the new part so it looks like this: cansas-is-good-for-you?m=2&id=2-0-0-0&sid=cansas.
How can I forward it when I have several dynamic parameters in the URL string? Example on pages I need to forward:
/cansas.php?m=2&id=2-0-0-0&sid=cansas
/cansas.php?m=2&id=2-1-0-0&sid=cansas
/cansas.php?m=2&id=2-2-0-0&sid=cansas
Any help would be greatly appreciated :)
Maybe it isn't possible to do it this way? The way I have set it up at the moment is that I want to use new URLs called /cansas-is-good-for-you which reads from the source /cansas.php?m=2&id=2-0-0-0&sid=cansas, but the URL shown in the browser should be: /cansas-is-good-for-you. I need to forward that old cansas.php? URL to the new URL :)
You need to check the query of a URL with the RewriteCond directive as the RewriteRule directive only handles the URL path:
RewriteCond %{QUERY_STRING} ^m=2&id=2-0-0-0&sid=cansas$
RewriteRule ^cansas\.php$ /cansas-is-good-for-you? [L,R=301]
If you want to check for just one parameter, use this:
RewriteCond %{QUERY_STRING} ^([^&]*&)*sid=cansas(&.*)?$
RewriteRule ^cansas\.php$ /cansas-is-good-for-you? [L,R=301]
And to do this just for initial requests, you need to check the request line:
RewriteCond %{THE_REQUEST} ^GET\ /cansas\.php
RewriteCond %{QUERY_STRING} ^([^&]*&)*sid=cansas(&.*)?$
RewriteRule ^cansas\.php$ /cansas-is-good-for-you? [L,R=301]

Resources