rewrite url with parameters in .htaccess - php

I have this incoming url where the last part is dynamic.
https://www.rishi.com/q/CuYuGy
and want to rewrite the url to below link.
https://rishi.com/test.php
where it gets the dynamic part and performs action
so far i have done this
RewriteCond %{HTTP_HOST} ^rishi.com/q/%{QUERY_STRING}$
RewriteRule ^$ {HTTP_HOST} ^rishi.com/test.php [L,R=301]
please help me resolve it.
Thanks

Try this
RewriteRule ^q/([a-zA-Z0-9-/]+)$ test.php?query_string=$1
RewriteRule ^q/([a-zA-Z0-9-/]+)/$ test.php?query_string=$1

Try this below htaccess code. It should help you
RewriteCond %{REQUEST_URI} ^/q/(.*)$
RewriteRule ^(.*)$ /test.php [L,R=301]

Related

Simple mod rewrite query string but still failed

I want to create .htaccess mod rewrite but still have problem.
Suppose i created friendly URL like this :
mydomain.com/mypage/12345/title-of-news
I want that friendly URL process the below URL in hidden process :
mydomain.com/index.php?p=mypage&idnews=12345
Values of "p" and "idnews" are dynamic, so they will have different value.
I tried below code but still didn't work. Anything wrong?
RewriteBase /
RewriteCond %{QUERY_STRING} p=(\w+)&idnews=(\d+)
RewriteRule ^index.php /%1/%2 [L,R=301]
Any help would be appreciated. Sorry if this is duplicated question, if don't mind please tell me the answer link. Thanks a lot.
I think you don't need the RewriteCond you write there.
And you can use following rule
RewriteRule ^/?([0-9a-zA-Z_.-]+)/([0-9]+)/([0-9a-zA-Z_.-]+)$ index.php?p=$1&idnews=$2 [L]
If you want to change index.php?p=foo&idnews=bar to /foo/bar , try theses rules :
RewriteEngine on
##externally redirect "/index.php?p=foo&idnews=bar" to "/foo/bar"##
RewriteCond %{THE_REQUEST} /index\.php\?p=([^&]+)&idnews=([^&\s]+) [NC]
RewriteRule ^ /%1/%2? [L,R,NC]
##internally redirect "/foo/bar" to "/index.php?p=foo&idnews=bar"##
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/([^/]+)/?(.*)?$ /index.php?p=$1&idnews=$2 [NC,L]

.htaccess rewrite get parameter inside the URL

I'm having troubles to rewrite a get parameter inside the URL.
I need to replace to 0 the value or delete at all next request "filter=all" from urls across entire site.
Example of entire requst:
mysite.com/?filter=all&search_text=&type=xx&pagination=10&search_page=10&cs_loc_max_input=300&cs_loc_incr_step=1&goe_location_enable=off&submit=
I've tried:
RewriteRule ^filter=0$ filter=all&%{QUERY_STRING} [L,NC]
OR
RewriteCond %{QUERY_STRING} ^filter=all$
RewriteRule ^filter=0$ ?filter=all&%{QUERY_STRING}[L,NC]
OR
RewriteCond %{QUERY_STRING} ^(.*[&?]|)filter=(all|all2)([&?].*|)$
RewriteRule ^(.*)$ $1?filter=0&%{QUERY_STRING} [R=301,NC,L]
And few ways more, but nothing works as expected... Please, need a help.
Thanks
You can use:
RewriteCond %{QUERY_STRING} ^(.*)filter=all(.*)$ [NC]
RewriteRule ^ %{REQUEST_URI}?%1filter=0%2 [R=301,L]
If you try to rewrite without redirect. Use [L] instead of [R=301,L].

basic URL Rewriting in php issue

I have url coming on my web page like:
www.abc.com/product_list.php?id=12&pId=1&gpId=0
I want it to show like:
www.abc.com/product_list.php/12/1/0
i know this is very basic question but i am new in php.
your help will be really appreciated.
I did experiment like this but its not working: (in my htaccess file by googling)
RewriteRule ^product_list.php /product_list.php/id=$1/pId=$2/gpId=$3 [NC]
My .htaccessfile:
RewriteEngine On
RewriteCond %{ENV:REDIRECT_STATUS} 200
RewriteRule ^ - [L]
RewriteCond %{HTTP_USER_AGENT} (google|yahoo|msn|aol|bing) [OR]
RewriteCond %{HTTP_REFERER} (google|yahoo|msn|aol|bing)
RewriteRule ^([^/]*)/$ hamper-anastassia.php?$1 [L]
RewriteRule index.html$ /index.php
RewriteRule about.html$ /about.php
RewriteRule products.html$ /products.php
RewriteRule partners.html$ /partners.php
RewriteRule career.html$ /career.php
RewriteRule contact.html$ /contact.php
RewriteRule products_(.*)_(.*).html$ products.php?id=$2 [L]
RewriteRule ^product_list/([0-9]+)/([0-9]+)/([0-9]+)/?$ /product_list.php?id=$1&pId=$2&gpId=$3 [NC]
You should remove the .php
A simple example to capture the first parameter.
RewriteRule ^products/([0-9]+)/?$ show_a_product.php?product_id=$1
More here
https://www.addedbytes.com/articles/for-beginners/url-rewriting-for-beginners/
Basically every pairing of brackets catches a variable, this is where the $1, $2, $3 comes from.
RewriteRule ^product_list.php/([0-9]+)/([0-9]+)/([0-9]+)/?$ /product_list.php?id=$1&pId=$2&gpId=$3 [NC]
As Geoffrey mentions you could go one step further and remove the need for .php but that's up to you, e.g:
RewriteRule ^product_list/([0-9]+)/([0-9]+)/([0-9]+)/?$ /product_list.php?id=$1&pId=$2&gpId=$3 [NC]
Meaning you could visit /product_list/12/1/0
Please remove/comment everything in your .htaccess file then type only these two statements
RewriteEngine on
RewriteRule ^products/([0-9]+)/([0-9]+)/([0-9]+)/?$ products.php?id=$1&pid=$2&gpid=$3
Your url should be like this:
/products/1/2/3
If your still getting and error check your mod_rewrite configurations

.htaccess rewriterule and condition

Guys I am trying to change my url using .htaccess but getting one problem
My URL looks like this
www.example.com/web-search.php?q=car&limit=15
I want to turn it like that
www.example.com/web/cars/15
but not succeeding I don't get the problem please help me. Thanks
Here is my .htaccess code
RewriteEngine On
RewriteCond %{THE_REQUEST} ^(GET|POST)\ /web-search\.php\?q=(.*)\&limit=(.*?)\ HTTP
RewriteRule ^ /%2/%3? [R=301,L]
RewriteRule ^ web/([^/]*)/([^/]*)$ /web-search.php?q=$1&limit=$2 [L]
It works it I remove "web/" from the rewrite url but then url looks like
www.example.com/cars/15
which I don't want I want to add web/ also. Thanks in advance
Update 1:
This causing the problem it is above the mentioned rewrite rule
RewriteEngine On
RewriteCond %{THE_REQUEST} ^(GET|POST)\ /search\.php\?q=(.*)\&limit=(.*)\&siz=(.*?)\ HTTP
RewriteRule ^ /%2/%3/%4? [R=301,L]
RewriteRule ^([^/]*)/([^/]*)/([^/]*)$ /search.php?q=$1&limit=$2&siz=$3 [L]
This is caused by two oversights. The first rule does not add "web" to the url. The second rule contains a rogue space between the ^ and the rest of the regex, causing it to return an error.
RewriteEngine On
RewriteCond %{THE_REQUEST} ^(GET|POST)\ /web-search\.php\?q=(.*)\&limit=(.*?)\ HTTP
RewriteRule ^ /web/%2/%3? [R=301,L]
RewriteRule ^web/([^/]*)/([^/]*)$ /web-search.php?q=$1&limit=$2 [L]
Remember to clear your browser cache before testing this, as your browser might have cached the old, wrong, redirect.

RewriteRule not redirecting

Due to request, every page needs to be prefixed with "index.php"; if the path was first /it, now it needs to be /index.php/it.
I have tried changing base url in settings.php. But it doesnot works.Tried rewriting in .htaccess also. I have tried the following code
RewriteBase /
RewriteCond %{HTTP_HOST} ^http://www.example.com/index.php [NC]
RewriteRule ^(.*)$ http://www.example.com/index.php [L,R=301]
But it doesnot works. Somebody help me please. Thanks in advance..
Make sure you have mod_rewrite enabled and try with this
RewriteEngine on
RewriteBase /
RewriteRule ^(.*)$ http://www.example.com/index.php/$1 [L,R=301]
Remember that the $1 refers to the (.*) you have captured in the URL
You are not using your capture group (.*). You can refer to it using $1
RewriteRule ^(.*)$ http://www.example.com/index.php/$1 [L,R=301]

Categories