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/
Related
I have this
RewriteCond %{THE_REQUEST} \s\/(\w+).php [NC]
RewriteRule ^ /%1 [R=301,L]
RewriteRule ^(\w+)$ /$1.php [L]
This removes .php extension and if user directly type test.php in browser will load test. So far so good.
Now I trying to make also if user type in browser this http://example.com/test.php?page=something to redirect and load http://example.com/test/something. Is this possible?
You need additional rules for handle a parameter:
RewriteEngine On
# handle one parameter redirect rule
RewriteCond %{THE_REQUEST} \s/+(\w+)\.php\?id=([^\s&]+) [NC]
RewriteRule ^ /%1/%2? [R=302,L,NE]
RewriteCond %{THE_REQUEST} \s/+(.+?)\.php[\s?] [NC]
RewriteRule ^ /%1 [R=302,NE,L]
RewriteCond %{DOCUMENT_ROOT}/$1\.php -f
RewriteRule ^(\w+)/([\w-]+)/?$ $1.php?page=$2 [L,QSA,NC]
RewriteCond %{DOCUMENT_ROOT}/$1\.php -f
RewriteRule ^(\w+)/?$ $1.php [L]
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.
current URL:
domain/index.php?id=p123
I want:
add www
remove:index.php
remove 'id' form URL
I do it this way:
RewriteCond %{THE_REQUEST} /(?:index\.php)?\?id=([^\s&]+) [NC]
RewriteRule ^ /%1? [R=302,L,NE]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?id=$1 [L]
RewriteCond %{THE_REQUEST} /(?:index\.php)?\?id=([^\s&]+) [NC]
RewriteRule ^ /%1? [R=302,L,NE]
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]
it works if domian is without sub directory.
how can I change this code to support sub directory too like this:
domian/subdirectory/index.php?id=p123
I'll go about it this way:
First, redirect non-www URLs to www ones:
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTPS}s ^on(s)|
RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
Rewrite the friendly URLs to respective index.php files:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*/)?([^/]+)$ /$1index.php?id=$2 [L]
Next, handle the raw URIs with index.php in them:
RewriteCond %{THE_REQUEST} ^GET\ /(.*/)?(?:index\.php)?\?id=([^\s&]+) [NC]
RewriteRule ^ /%1%2? [R=301,L]
The last rule will also take care of URLs like domain.com/dir/?id=sth and domain.com/dir/index.php?id=sth
You can accomplish this with the following changes:
RewriteCond %{THE_REQUEST} \ /(.*/|)(?:index\.php)?\?id=([^\s&]+) [NC]
RewriteRule ^ /%1%2? [R=302,L,NE]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !index\.php
RewriteRule ^(.*/|)([^/]*)$ $1index.php?id=$2 [L]
We know that %{THE_REQUEST} has the format GET /path/to/index.php?id=12312412 HTTP/1.1 or something along those lines. In the first rule we match \, which is a literal space, then the beginning slash that is always there. (.*/|) will match everything until index.php, or exactly nothing. The same trick we do in the second rule. I have added RewriteCond %{REQUEST_URI} !index\.php to prevent an infinite loop when index.php does not exist for a particular directory.
My request will come like /insights/papers-articles.html?view=papers&p=10&/abcdefgh&indcal=1
When i hit this url, it redirect me to /insights/papers-articles/papers-abcdefgh-3.html
I want indcal parameter so url will be /insights/papers-articles/papers-abcdefgh-10.html?indcal=1
I have used following rules :
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+(insights)/(papers-articles)\.html\?view=([^&\s]+)&p=([^&\s]+)&/([^&\s]+)&indcal=([0-9]+) [NC]
RewriteRule ^ /%1/%2/%3-%5-%4.html\?indcal=%6 [R=301,L]
But "indcal" parameter is not comming, Please help.
TIA.
This is complete rules
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+(insights)/(papers-articles)\.html\?view=([^&\s]+)&p=([^&\s]+)&/([^&\s]+)$ [NC]
RewriteRule ^ /%1/%2/%3-%5-%4.html? [R=301,L]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+(insights)/(papers-articles)\.html\?view=([^&\s]+)&p=([^&\s]+)&title=([^&\s]+) [NC]
RewriteRule ^ /%1/%2/%3-%5-%4.html? [R=301,L]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+(insights)/(papers-articles)\.html\?view=([^&\s]+)&q=([^&\s]+)&/([^&\s]+) [NC]
RewriteRule ^ /%1/%2/%3-%5-%4.html? [R=301,L]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+(insights)/(papers-articles)\.html\?view=([^&\s]+)&q=([^&\s]+)&title=([^&\s]+) [NC]
RewriteRule ^ /%1/%2/%3-%5-%4.html? [R=301,L]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+(insights)/(papers-articles)\.html\?p=([^&\s]+)&/([^&\s]+) [NC]
RewriteRule ^ /%1/%2/papers-%4-%3.html? [R=301,L]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+(insights)/(papers-articles)\.html\?view=([^&\s]+)&p=([^&\s]+)&/([^&\s]+)&indcal=([0-9]+) [NC]
RewriteRule ^ /%1/%2/%3-%5-%4.html\?indcal=%6 [R=301,L]
Last rule is for above query.
Rearrange your rules like this:
RewriteEngine On
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+(insights)/(papers-articles)\.html\?view=([^&\s]+)&p=([^&\s]+)&/([^&\s]+)&indcal=([0-9]+) [NC]
RewriteRule ^ /%1/%2/%3-%5-%4.html\?indcal=%6 [R=301,L]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+(insights)/(papers-articles)\.html\?view=([^&\s]+)&p=([^&\s]+)&/([^&\s]+)$ [NC]
RewriteRule ^ /%1/%2/%3-%5-%4.html? [R=301,L]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+(insights)/(papers-articles)\.html\?view=([^&\s]+)&p=([^&\s]+)&title=([^&\s]+) [NC]
RewriteRule ^ /%1/%2/%3-%5-%4.html? [R=301,L]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+(insights)/(papers-articles)\.html\?view=([^&\s]+)&q=([^&\s]+)&/([^&\s]+) [NC]
RewriteRule ^ /%1/%2/%3-%5-%4.html? [R=301,L]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+(insights)/(papers-articles)\.html\?view=([^&\s]+)&q=([^&\s]+)&title=([^&\s]+) [NC]
RewriteRule ^ /%1/%2/%3-%5-%4.html? [R=301,L]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+(insights)/(papers-articles)\.html\?p=([^&\s]+)&/([^&\s]+) [NC]
RewriteRule ^ /%1/%2/papers-%4-%3.html? [R=301,L]
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