url should rewrite if page and query string is match - php

I want to redirect page if my url and query string is matched i have try much but no success, can you please told me where i done mistake.
My url is
http://xxx.xxx.xxx.xx/search/videos?search_query=xxx+xxx
and I want to redirect (rewrite using .htaccess) it to
http://xxx.xxx.xxx.xx/search/videos/xxx+xxx
I have tryed
RewriteCond %{QUERY_STRING} ^search_query=([A-Za-z0-9]*)$
RewriteRule ^search/videos$ search/videos/$1 [R=301,L]
RewriteCond %{QUERY_STRING} "search_query=" [NC]
RewriteRule ^(.*)$ search/videos/$1? [L,QSA]
RewriteCond %{QUERY_STRING} ^search_query=$1 [NC]
RewriteRule ^(.*)$ search/videos/$1? [L,QSA]
What i am doing wrong?

You can use the following rule
RewriteEngine on
#terminate the 2nd rewrite iteration
RewriteCond %{ENV:REDIRECT_STATUS} 200
RewriteRule ^ - [L]
# redirect "/search/videos?search_query?foobar" to "/search/videos/foobar"
RewriteCond %{QUERY_STRING} ^search_query=(.+)$
RewriteRule ^search/videos$ /search/videos/%1? [R=301,L]
#internally map "/search/videos/foobar" to "/search/videos?search_query?foobar"
RewriteRule ^search/videos/([^/]+)/?$ /search/videos/?search_query=$1 [NC,L]

Related

Redirecting to new address if there's specific get parameter

I'm trying to redirect this address
https://www.example.com/dire-something/simething-else?source=post_page---------------------------/
to
https://www.example.com/dire-something/simething-else
Here is what i came up with:
RewriteRule ^(.*)?source=post_page(.*) /$1 [L,R=301]
but it doesn't work.
you can try this
RewriteEngine On
RewriteCond %{QUERY_STRING} ^source=post_page$
RewriteRule ^(.*)$ %{REQUEST_URI}\? [R,L]
You can't match query strings within RewriteRule directive. Use RewriteCond for this purpose:
RewriteCond %{QUERY_STRING} \bsource=post_page
RewriteRule . /$0 [QSD,L,R=301]
RewriteCond %{QUERY_STRING} ^source=post_page([^&]+) [NC]
RewriteRule ^(.*)$ %{REQUEST_URI}\? [R,L]

mod_rewrite GET form to clean URL

I have tried searching but only came up with this link which I can get to work, just not correctly to apply to my current situation.
RewriteCond %{REQUEST_URI} ^/highscores/personal/$
RewriteCond %{QUERY_STRING} ^user1=(.*)$
RewriteRule ^(.*)$ /highscores/personal/%1? [R=301,L,QSA]
RewriteRule ^highscores/personal/(.*)/vs/(.*) personal.php?user1=$1&user2=$2 [L,QSA]
RewriteRule ^highscores/personal/(.*) personal.php?user1=$1 [L,QSA]
RewriteRule ^highscores/skill/(.*) highscores.php?skill=$1 [L,QSA]
RewriteRule ^highscores/(.*) highscores.php?$1 [L,QSA]
As of right now, it will redirect
http://localhost/highscores/personal/?user1=test
to
http://localhost/highscores/personal/test
like it should.
But I have a compare function which submits a GET request like:
http://localhost/highscores/personal/?user1=test&user2=test2
which needs to come out like
http://localhost/highscores/personal/test/vs/test2
but it comes out like
http://localhost/highscores/personal/test&user2=test2
Edit: Resolved using help from this htaccess tester
RewriteCond %{REQUEST_URI} ^/highscores/personal/$
RewriteCond %{QUERY_STRING} ^user1=([^&]+)&user2=(.*)$
RewriteRule ^(.*)$ /highscores/personal/%1/vs/%2? [R=301,L]
RewriteCond %{REQUEST_URI} ^/highscores/personal/$
RewriteCond %{QUERY_STRING} ^user1=(.*)$
RewriteRule ^(.*)$ /highscores/personal/%1 [R=301,L]
You need to edit your rule
RewriteRule ^highscores/personal/(.*)/vs/(.*) personal.php?user1=$1&user2=$2 [L,QSA]
to
RewriteRule ^highscores/personal/([^/]+)/vs/(.*) personal.php?user1=$1&user2=$2 [L,QSA]
because of greedy regex. You need to break (.*) section if / is found

url redirection issue using .htaccess

Below are the things i need to convert using .htaccess
If url is sample.xxx.com means i need to convert it to www.xxx.com/domain/sample
I url is sample.xxx.com/category/34/electonics.html means i need to convert it to www.xxx.com/domain/sample/category/34/electronics.html
3.After converting those things again i need to build request to the necessary pages
I have the following rule
RewriteEngine On
RewriteCond %{HTTP_HOST} ^([^\.]+)\.example\.com$ [NC]
RewriteCond %1 !^(www|ftp|mail)$ [NC]
RewriteRule ^(.*)$ http://www.example.com/domain/%1/$1 [P,QSA]
RewriteRule ^domain/([a-zA-Z0-9_\-]+)$ index.php?domain=$1 [Nc,L]
RewriteRule ^domain/([a-zA-Z0-9_\-]+)/$ index.php?domain=$1 [Nc,L]
RewriteRule ^domain/([a-zA-Z0-9_\-]+)/news/([a-zA-Z0-9_\-]+)/([0-9]+)_([0-9]+)/([a-zA-Z0-9_\-]+)\.html$ news.php?domain=$1&category=$4&news=$3 [Nc,L]
RewriteRule ^domain/([a-zA-Z0-9_\-]+)/category/([0-9]+)/([a-zA-Z0-9_\-]+)\.html$ category.php?domain=$1&category=$2 [Nc,L]
RewriteRule ^domain/([a-zA-Z0-9_\-]+)/gallery/([0-9]+)/([a-zA-Z0-9_\-]+)\.html$ gallery.php?domain=$1&category=$2 [Nc,L]
RewriteRule ^domain/([a-zA-Z0-9_\-]+)/video/([0-9]+)/([a-zA-Z0-9_\-]+)\.html$ video.php?domain=$1&category=$2 [Nc,L]
But it fails
If i change line 4 to
RewriteRule ^(.*)$ http://www.example.com/domain/%1/$1 [NC,QSA]
means it will work but the browser shows the reconstructed url.
Anyone spot my error please.
If you keep http:// in target URI then it will indeed be full redirect.
Try this:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^(www|ftp|mail)\. [NC]
RewriteCond %{HTTP_HOST} ^([^.]+)\.example\.com$ [NC]
RewriteRule ^((?!domain/).*)$ domain/%1/$1 [L,NC]
RewriteRule ^domain/([\w-]+)/news/([\w-]+)/([0-9]+)_([0-9]+)/([\w-]+)\.html$ news.php?domain=$1&category=$4&news=$3 [NC,L]
RewriteRule ^domain/([\w-]+)/category/([0-9]+)/([\w-]+)\.html$ category.php?domain=$1&category=$2 [NC,L]
RewriteRule ^domain/([\w-]+)/gallery/([0-9]+)/([\w-]+)\.html$ gallery.php?domain=$1&category=$2 [NC,L]
RewriteRule ^domain/([\w-]+)/video/([0-9]+)/([\w-]+)\.html$ video.php?domain=$1&category=$2 [NC,L]
RewriteRule ^domain/(.+)/?$ index.php?domain=$1 [NC,L]

Why does my htaccess file does not redirect links without http

I have been trying to make redirects through my website, but currently when i give the redirect link it does not correctly redirect unless you add the http
The redirect url
tracker02.globaltracking.com/ric?track=100&go=www.fox.com
.htaccess file
RewriteEngine On
RewriteBase /
RewriteCond %{ENV:PROCESSED} TRUE
RewriteRule (.*) $1 [S=2]
RewriteCond %{QUERY_STRING} ^.*([^?&]*)go=
RewriteCond %{QUERY_STRING} ^.*([^?&]*)track=
RewriteRule ^(rd) /redirect.php
RewriteCond %{HTTP_COOKIE} id=([^;]+) [NC]
RewriteRule ^(rd)$ https://%{HTTP_HOST}/pros_redirect.php?atid=%1 [R=permanent,QSA,L]
RewriteCond %{HTTP_COOKIE} !id=[^;]+ [NC]
RewriteCond %{QUERY_STRING} ^.*&go=([^?&]*)\??([^&]*)&*.*$
RewriteRule ^ric$ ${allowed_sites:%1|/ric.gif}?%2 [NE,E=PROCESSED:TRUE]
RewriteCond %{ENV:PROCESSED} TRUE
RewriteCond %{QUERY_STRING} ^(.*)%26(.*)$
RewriteRule (.*) $1?%1&%2 [NE,N]
RewriteCond %{ENV:PROCESSED} TRUE
RewriteRule (.*) $1 [NE,L]
RewriteRule ^(ric)$ /ric.gif [L]
can anybody help me on this.. the error i get is
The requested URL /www.fox.com was not found on this server.

htaccess remove www and redirect to accessed url

I am using codeigniter framework i need to force to remove www from the url so I am using this code
RewriteEngine on
RewriteCond $1 !^(index\.php|css|images|js|plugins|scripts|fancybox|uploads|mobile|robots\.txt)
RewriteRule ^(.*)$ /framework/index.php?/$1 [L]
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1%{REQUEST_URI}/$1 [R=301,QSA,NC,L]
This code is forcing removal of www. but the problem is when a user access a link with www
eg:www.mydomain.com/framework/article/sometestarticle368/
It is redirecting to
www.mydomain.com/framework/
How can i fix this ?
Change the order of your rules:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^(.*)$ http://%1%{REQUEST_URI}/$1 [R=301,NE,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond $1 !^(index\.php|css|images|js|plugins|scripts|fancybox|uploads|mobile|robots\.txt)
RewriteRule ^(.*)$ /framework/index.php?/$1 [L,QSA]
Otherwise your 2nd rules runs first and change the URI to /framework/... before the www removal rule..
Try this:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.yoursite\.com [NC]
RewriteRule (.*) http://yoursite.com/$1 [R=301,L]
Thanks

Categories