.htaccess RewriteRule is not working - .htaccess

RewriteRule new/$ /search.php?category=1
RewriteRule new/\?(.+)$ /search.php?category=1&$1
I'm trying to do something like this, if the following address link is accessed,
http://onlineshop.com/new/
http://onlineshop.com/new/?price_max=30
then it will open this link,
http://onlineshop.com/new/search.php?category=1
http://onlineshop.com/new/search.php?category=1&price_max=30
Unfortunately it is not working this way.

A RewriteRule won't naturally catch query string parameters, you must use this kind of .htaccess :
RewriteCond %{QUERY_STRING} ^(.*)$
RewriteRule ^new/$ /search.php?category=1&%1

You can just simply redirect from /new to /search.php with QSA flag and Apache will append the existing query string. Something like this will work for you:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteRule ^new/?$ /search.php?category=1 [L,QSA,NC]

Related

RewriteRule for specific Query String

I’d like to use .htaccess to rewrite a URL like this
https://example.com/pages/games/demons-souls/?page=mods
to look like this
https://example.com/games/demons-souls/mods
I managed to hide the pages folder in the URL with this code:
Options +MultiViews
RewriteEngine On
# Rewrite "/games/<anything>" to "/pages/games/<anything>"
RewriteCond %{REQUEST_URI} !^/pages/games
RewriteRule ^games($|/.*) pages/$0
But I need a proper RewriteRule for the Query String. I tried this but it didn’t work ...
RewriteRule ^([^/]*)/([^/]*)$ $1/index.php?page=$2 [L,QSA]
I’d really appreciate some help.
You can have it like this:
Options +MultiViews
RewriteEngine On
# Rewrite "/games/<foo>/<bar>" to "/pages/games/<foo>/?page=<bar>"
RewriteRule ^(games/[\w-]+)/([\w-]+)/?$ pages/$1/?page=$2 [L,NC,QSA]
# Rewrite "/games/<anything>" to "/pages/games/<anything>"
RewriteRule ^games($|/.*) pages/$0 [L,NC]

How to create clean url using .htaccess for multiple parameters

This is my .htaccess code to rewrite clean url.
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+download\.php\?id=([^\s&]+) [NC]
RewriteRule ^ download/%1? [R=301,L]
RewriteRule ^download/([^/]+)/?$ download.php?id=$1 [L,QSA]
This code works fine for single parameter in url. For example it rewrites www.mysitename.com/download.php?id=123 to www.mysitename.com/download/123
But when I tried to pass multiple parameters in url, all I got are errors. Searched various resources and related questions but didn't got proper solution.
I need a url like www.mysitename.com/download/123/file-name instead of www.mysitename.com/download.php?id=123&name=file-name
I guess I've to use something thing like this RewriteRule ^(.*)/(.*)$ download.php?id=$1&name=$2. But while implementing I'm getting 404 error. How can I alter My code to pass multiple urls. Thanks in advance.
You just need to add a parameter in your rule
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+download\.php\?id=([0-9]+)&name=([^\s&]+)\s [NC]
RewriteRule ^ download/%1/%2? [R=301,L]
RewriteRule ^download/([0-9]+)/([^/]+)/?$ download.php?id=$1&name=$2 [L]

Not redirecting for clean URL

So I'm running into an issue here that I believe I have correct yet it's just not working for me here.
So simple enough I want me current URL which is:
tremorelights.com/chandelier-c-18.html?page=2&osCsid=vtemi4nqlioftbteam6nap0s77
To look like this URL:
tremorelights.com/chandelier/2/
So I'm trying to redirect it to the clean URL and this is what I have now.
RewriteEngine On
RewriteBase /
RewriteRule ^chandelier-c-18.html?page=$1 chandeliers/([0-9]+) [QSA,L,R=301]
Yet this is not working at all. When I manually input the clean URL it does not give me a 404 error but it does not take me to any other page like it should.
You can match against the query string in a rewrite rule, what you want to match against is the %{THE_REQUEST} variable here.
Try:
RewriteEngine On
RewriteBase /
RewriteCond %{THE_REQUEST} \ /+chandelier-c-18.html\?page=([0-9]+)
RewriteRule ^ chandeliers/%1? [QSA,L,R=301]
then maybe:
RewriteRule ^chandeliers/([0-9]+) chandelier-c-18.html?page=$1 [L]
Change your rule to this:
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /
RewriteRule ^chandeliers/([0-9]+)/?$ chandelier-c-18.html?page=$1&osCsid=vtemi4nqlioftbteam6nap0s77 [QSA,L,NC]

url rewriting and moving of questionmark

I've tried all manor of rules and just can work this one out. If there someone who can solve this?
I need to turn this
http://localhost/search/?terms=foobar
into this
http://localhost/index.php?m=search&terms=foobar
Where "foobar" is really ([^/]*), ie can be anything, it's a search string.
The initial URL has a "?" in it due to the GET method of HTML forms. Although it would be easier without that "?" it's not something that can be done?
I've tried the following with no luck.
#RewriteRule ^search/\?terms\=([^/]*)$ index.php?m=search&terms=$1 [L]
Thanks.
For query string you need to use %{QUERY_STRING}:
RewriteCond %{QUERY_STRING} ^terms=(.*)$ [NC]
RewriteRule ^search/?$ index.php?m=search&terms=%1 [NC,L]
You cannot match QUERY_STRING in RewriteRule. Have your code like this:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteCond %{QUERY_STRING} ^(.+)$
RewriteRule ^(search)/?$ index.php?m=$1 [L,NC,QSA]
Because of QSA flag %{QUERY_STRING} will automatically be carried over to new URL.

Htaccess redirect url with raw php url containing variables

I'm currently using a CMS that rewrites all URLs and currently I have a URL like this:
http://www.domain.com/folder/?user=user1
I'm looking to have that rewrite to: index.php?r=search&term=$1
Would something like this work?
RewriteRule ^/?user=(.*) index.php?r=search&term=$1 [L]
Seems to be giving me trouble. Any suggestions?
The query string is not part of the URI-path test it in the rule. It is at QUERY_STRING variable.
You may try this:
Options +FollowSymlinks -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{QUERY_STRING} user=([^/]+)/? [NC]
RewriteRule ^folder/? /index.php?r=search&term=%1 [L,NC]
try ( not tested)
RewriteEngine On
Options +FollowSymLinks
RewriteRule ^folder1/?user=(.*)$ index.php?r=search&term=$1 [R=301,L]

Resources