I'm trying to redirect urls from query string using this code:
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /([^?#\ ]*)\?[^\ ]*\ HTTP/
RewriteRule (.+) http://www.example.com/$1? [R=301,L]
This worked well for:
www.example.com/mypage.html?gclid=xxx
But it does not work for:
www.example.com/?gclid=xxx
www.example.com/mypage.html/?gclid=xxx
Also tried unsuccessfully [1]:this answer [question]:Redirecting in HTACCESS from Query String to URL
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /\?gclid=([a-z]+)&type=([a-z\-]+) [NC]
RewriteRule ^ /%1/%2? [L,R=301]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /\?gclid=([a-z]+)($|\ ) [NC]
RewriteRule (.+) http://www.example.com/$1? [R=301,L]
What changes could I do?
thank you very much
Related
I have searched all over Stackoverflow and Googled wide and far, but I cannot get this seemingly simple task done.
I'm trying to set a rule in htaccess to clean the URL by removing the query string, but leaving its value intact:
http://example.local/?p=subscribe
Becomes:
http://example.local/subscribe
I have tried these various methods:
RewriteEngine on
RewriteCond %{QUERY_STRING}
RewriteRule (.*) /$1? [R=301,L]
or,
RewriteCond %{QUERY_STRING} ^p=$ [NC]
RewriteRule ^(/?)?$ $1? [R=301,L,NC]
or,
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /\?([^\ ]+)
RewriteRule ^$ /%1? [R=301,L]
But nothing works!
Any help will be greatly appreciated.
Have it like this in your .htaccess:
RewriteEngine On
RewriteCond %{THE_REQUEST} \s/([^?]*?)/?\?p=([^\s&]+)\s [NC]
RewriteRule ^ /%1/%2? [R=301,L,NE]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([\w-]+)/?$ ?p=$1 [L,QSA]
You can use % to retrieve a rewriteCond Variables
RewriteCond %{QUERY_STRING} ^p=(.*)$ [NC]
RewriteRule ^(.*)?$ %1? [R=301,L,NC]
Will convert any url http:/site.com/url?qs to http:/site.com/qs
I have 2 type of url in my page.
1- http://www.example.com/index.php?id=12
I want always this:
http://www.example.com/12
But for anything else like:
2- http://www.example.com/index.php?id=12&id2=123&asd=qwe&svd=sdf...
I want it with no change:
http://www.example.com/?id=12&id2=123&asd=qwe&svd=sdf...
SO I want only redirect for http://www.example.com/index.php?id=12
I try this
RewriteCond %{THE_REQUEST} ^GET\ /(.*/)?(?:index\.php)?\?id=([^\s&]+) [NC]
RewriteRule ^ /%1%2? [R=301,L]
it works only for first (http://www.example.com/index.php?id=12)
but for second,it redirect to (http://www.example.com/***)
I read in stackoveflow I must use [QSA,L]
So I try this:
RewriteCond %{THE_REQUEST} ^GET\ /(.*/)?(?:index\.php)?\?id=([^\s&]+) [NC]
RewriteRule ^ /%1%2? [QSA,L]
But I got 500 error (Internal Server Error)
this is my complete code:
RewriteEngine On
RewriteBase /
RewriteCond %{THE_REQUEST} ^GET.*index\.php [NC]
RewriteRule (.*?)index\.php/*(.*) /$1$2 [R=301,NE,L]
RewriteCond %{HTTP_HOST} !^$
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTPS}s ^on(s)|
RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
RewriteCond %{THE_REQUEST} ^GET\ /(.*/)?(?:index\.php)?\?id=([^\s&]+) [NC]
RewriteRule ^ /%1%2? [R=301,L]
---EDIT ---
I could do it with this
RewriteRule ^index.php/([^/]+)/?([^/]*) /index.php?id=$1 [NC]
for example www.example.com/123 works!
but www.example.com/index.php?id=123 not redirect to www.example.com/12
To convert http://example.com/index.php?id=foo to http://example.com/foo you can use the following rules in root/.htaccesso :
RewriteEngine on
#1)Redirect "/index.php?id=foo" to "/foo"#
RewriteCond %{THE_REQUEST} /(?:index\.php)?\?id=([^\s&]+)\sHTTP [NC]
RewriteRule ^ /%1? [L,R]
#2)The rule bellow will internally map "/foo" to "/index.php?id=foo"#
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/?$ /index.php?id=$1 [L]
[Tested] This works on my apache server.
i want to ask that when i upload .htaccess file to my hostgator server my one of url is not working
url: email/admin/index.php
the htacces code here:
RewriteEngine on
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.php\ HTTP/
RewriteRule ^index\.php$ http://www.appynator.com/ [R=301,L]
RewriteCond %{HTTP_HOST} ^appynator\.com [NC]
RewriteRule (.*) http://www.appynator.com/$1 [L,R=301]
Try changing the first rule to:
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /(.*)index\.php(\?|\ )
RewriteRule ^ http://www.appynator.com/%1 [R=301,L]
I have URLs like the following:
mysite.com/eng?sentence=dog
and I want to rewrite them as
mysite.com/eng/dog
so I want to replace ?sentence= with a "/"
I have tried all of the following:
RewriteCond %{THE_REQUEST} ^GET\ sentence= [NC]
RewriteRule ^ /%1 [NC,L,NE]
RewriteCond %{THE_REQUEST} ^GET\ \?sentence= [NC]
RewriteRule ^ /%1 [NC,L,NE]
RewriteCond %{THE_REQUEST} ^GET\ \?sentence=([^/?&\s]+) [NC]
RewriteRule ^ /%1 [NC,L,NE]
RewriteCond %{THE_REQUEST} ^GET\s/+\?sentence=([^/?&\s]+) [NC]
RewriteRule ^ /%1 [NC,L,NE]
then I tried accessing the URL:
mysite.com/eng?sentence=cat
but it stays as it is. I assume my regex logic is not correct and the rewrite cond is never satisfied. I always have trouble with regex.
You can use this rule:
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+(eng)\?sentence=([^\s&]+) [NC]
RewriteRule ^ /%1/%2/? [L,R]
This will redirect http://mysite.com/eng?sentence=dog into http://mysite.com/eng/dog/
I have the following problem:
http://www.mydomain.com/articles.php?artid=89
should become: http://www.mydomain.com/mykeyword
I dont mind if the id remain in the url...like: http://www.mydomain.com/89/mykeyword
I have the following .htaccess for the moment:
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^(mydomain\.com)$ [NC]
RewriteRule ^(.*)$ http://www.%1/$1 [R=301,L]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+news\.php [NC]
RewriteRule ^ mykeyword [R=301,L]
RewriteRule ^mykeyword/?$ news.php [L,NC]
The above part works like a charm.
Any help will be deeply appreciated.
Regards, Zoran
Append these rule at the bottom of your .htaccess:
RewriteEngine On
RewriteBase /turkexpo/
RewriteCond %{HTTP_HOST} ^(webone\.gr)$ [NC]
RewriteRule ^(.*)$ http://www.%1/turkexpo/$1 [R=302,L]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+turkexpo/news\.php [NC]
RewriteRule ^ mykeyword [R=302,L]
RewriteRule ^mykeyword/?$ news.php [L,NC]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+turkexpo/articles\.php\?artid=([0-9]+) [NC]
RewriteRule ^ %1/mykeyword? [R=302,L]
RewriteRule ^([0-9]+)/mykeyword/?$ articles.php?artid=$1 [L,NC,QSA]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+turkexpo/companies\.php\?fairid=([0-9]+)&ehallid=([0-9]+) [NC]
RewriteRule ^ companies/%1/%2? [R=302,L]
RewriteRule ^companies/([0-9]+)/([0-9]+)/?$ companies.php?fairid=$1&ehallid=$2 [L,NC,QSA]
This will forward a URI of /89/mykeyword to /articles.php?artid=89 and externally redirect the opposite of it.
Once you make sure it is working change R=302 to R=301.