url rewriting and moving of questionmark - .htaccess

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.

Related

How to change my url

I have this url
http://example.com/profile.php?u=78
I wanted to changed it to be like this
http://example.com/78/
How would I do that?
Please Help
Thanks in advance
You can use this .htaccess in your root directory:
RewriteEngine on
RewriteRule ^(\d+)/?$ /profile.php?u=$1 [L]
RewriteCond %{QUERY_STRING} ^u=(\d+)$
RewriteRule ^profile.php$ /%1/? [R=302,L]
Change [R=302,L] to [R=301,L] when that work well
Try this to rewrite http://example.com/profile.php?u=78 into http://example.com/78/:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^(\w+)=(\w+)$
RewriteRule ^profile.php$ /%2/
RewriteRule (.*) $1? [R=permanent]
Line-by-line explanation:
Turn on rewriting functionality
Specify the condition to make sure the query string is in the form of foo=bar for the following rules to apply.
Rewrite profile.php?foo=bar into /bar/?foo=bar
Remove all query strings that may still be appended to the new URL like /bar/?foo=bar/. This rule essentially substitute the entire path (.*) with itself ($1), and emptying the query string (?). [R=permanent] performs a permanent redirect (301).
Also, as previously commented, please ensure that http://example.com/78/ is a valid URL.

Rewrite rule based on query string

I'm trying to redirect to the base url (www.example.com) requests that have a particular query string (when option is com_estateagent).
I've tried the following syntax:
RewriteEngine On
RewriteBase /
RewriteCond %{QUERY_STRING} ^option=com_estateagent$
RewriteRule .* index.php [R=301, L]
But it gets ignored.
Any suggestions?
EDIT
The url that I want to change is something like this:
http://www.example.com/subdirectory/index.php?option=com_estateagent...
I don't think you need the RewriteCond directive, wouldn't something like this work?
RewriteRule ^/.*option=com_estateagent /index.php[R=301,L]
This works
RewriteEngine On
RewriteBase /
RewriteCond %{QUERY_STRING} option=com_estateagent
RewriteRule .* subdirectory/index.php? [R=301, L]
Notice, the ? after index.php. That's important if you want to drop Query Parameters on redirect.

.htaccess RewriteRule is not working

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]

.htaccess not matching

htaccess not matching
RewriteCond %{REQUEST_URI} ^/catalog/products_in_scene.php?(.*)$
RewriteRule ^(.+) "/services/hpv/index.php?%1"
RewriteCond %{REQUEST_URI} ^/shop/derivation_tree.php?(.*)$
RewriteRule ^(.+) "/services/dt/index.php?%1"
The top one matches fine with all the GET variables, the second one matches and sends me to the right page but never sends it the GET variables; Why?
Remove the quote from your RewriteRule.
If it doesn't work, that mean there is something wrong in you php GET variable.
You'd do better to use the [QSA] flag:
RewriteRule ^/shop/derivation_tree.php /services/dt/index.php [QSA]
Change your code to this:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteRule ^catalog/products_in_scene\.php$ services/hpv/index.php [R,L,NC]
RewriteRule ^shop/derivation_tree\.php$ services/dt/index.php [R,L,NC]

URL rewriting variable name

I am trying to rewrite an URL so that a passed variable is replaced with a new id, for example;
wwww.domain.com/default.php?s=1&lang=en
To be rewritten to:
www.domain.com/default.php?id=1$lang=en
The s variable being replaced with id
I have tried:
RewriteEngine on
Options +FollowSymlinks
RewriteBase /
RewriteCond %{QUERY_STRING} ^s=(.+)$
RewriteRule ^s=(.+)$ id=$1 [R,NC,L]
also
RewriteRule ^s=(.+)$ id=%1 [R,NC,L]
no luck... what am I doing wrong?
Thanks for any help!!
This should work if even if you don't have s as the first variable. (Not sure what you really want.)
Options +FollowSymlinks
RewriteEngine On
RewriteCond %{QUERY_STRING} ^(.*&)*s=(.*)(&*)$
RewriteRule ^default.php$ default.php?%1&id=%2%3
Try this out:
RewriteRule ^default.php?s=([0-9]+)&lang=en$ default.php?id=$1&lang=en
Edit.
A more generic version.
RewriteRule ([^?]+)?s=([0-9]+)&lang=([A-z]{2})$ $1?id=$2&lang=$3

Resources