.htaccess rewriterule and condition - php

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.

Related

rewrite url with parameters in .htaccess

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]

How can i redirect /?lang=en to /en?

I'm having trouble redirecting a multilingual website one page. My redirection to htaccess doesn't operate properly. I need assistance with how this can be done. In my htaccess, when I add these two lines
RewriteCond %{QUERY_STRING} ^lang=en$
RewriteRule ^ /en? [R=301,L]
redirect working, but mysite.com/en show 404 not found. Someone help me with this?
Your redirect rule as shown in question is working fine. Bot you also need a rewrite rule to handle /en:
RewriteEngine On
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{QUERY_STRING} ^lang=([a-z]{2})$
RewriteRule ^ /%1? [R=301,L]
RewriteRule ^([a-z]{2})/?$ ?lang=$1 [QSA,L]
If your browsers ends up at mysite.com/en, your redirection does work, so your question is not how to redirect, but why there's no content at /en on your site. You need to investigate that.

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]

mod_rewrite: rewrite existing files to friendly links

I went through a bunch of websites and tutorials yet can't find a solution.
Following snippet works and http://example.com/page/pot return a pot.php content
RewriteEngine on
RewriteRule ^page/([^/]*)$ $1.php?page=$1 [L]
I can't get it to work the other way around
RewriteEngine on
RewriteRule ^([^/]*)/page$ $1.php?page=$1 [L]
Your current approach will cause infinite looping since Apache re-injects rewritten URI back for further rule processing.
You need to use THE_REQUEST variable for that like this:
RewriteEngine On
# external redirect from actual URL to pretty one
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+page/[^.]+\.php\?page=([^\s&]+) [NC]
RewriteRule ^ page/%1? [R=302,L]
# internal forward from pretty URL to actual one
RewriteRule ^page/([^/]*)/?$ $1.php?page=$1 [L,QSA,NC]
Try to use:
RewriteEngine on
RewriteRule ^([^/]+)/page$ $1.php?page=$1 [L]

.htaccess problem, need to add string to certain URL's

In my infinity stupidity I changed the permalinks on my wordpress blog a little while back and have now changed them again. The problem that I have now caused is that I have a few hundred URL's out there which no longer work.
For example this URL looks like the ones that used to work
http://www.lazygamer.net/the-evopoints-co-za-downloads-of-the-week-1305/
But you'll see that just gives you a 404 not found page now because my site expects the first subdirectory to be a category such as
http://www.lazygamer.net/xbox-360/the-evopoints-co-za-downloads-of-the-week-1305/
So now I want to put a htaccess rule in that checks to see if a category exists and if it doesn't then just add in something random to make the url resolve.
I'm pretty sure I can do this with a regular expression of sorts but I can't figure it out.
[Update] My current .htaccess file
RewriteEngine On
RewriteBase /
RewriteRule ^/([^/]+)/$ /category/$1/ [R]
RewriteRule ^index\.php$ - [L]
Options +FollowSymLinks
RewriteCond %{HTTP_HOST} ^www.lazygamer\.co.za$ [NC]
RewriteRule ^(.*)$ http://www.lazygamer.net/$1 [R=301,L]
RewriteCond %{HTTP_HOST} ^lazygamer\.co.za$ [NC]
RewriteRule ^(.*)$ http://www.lazygamer.net/$1 [R=301,L]
RewriteCond %{HTTP_HOST} ^lazygamer\.net$ [NC]
RewriteRule ^(.*)$ http://www.lazygamer.net/$1 [R=301,L]
Read up mod_rewrite to understand how to use these for other problems!
RewriteRule ^/([^/]+)/$ /category/$1/ [R]
That should do it.
Beware, this will redirect anything with one directory path in the URL to /category/{original_url_path}

Categories