i have to add if and else to my rewrite rule.
My rewrite is this
RewriteCond %{THE_REQUEST} \ /+catalogsearch/result/\?cat=(.*)&q=([^&\ ]+)
RewriteRule ^ /q_%2_cat_%1? [L,NE,R=301]
RewriteRule ^q_(.*)_cat_(.*)?$ /index.php/catalogsearch/result/?q=$1&cat=$2 [QSA,P,NC]
and i would like to change to this if cat is in null
RewriteCond %{THE_REQUEST} \ /+catalogsearch/result/\?cat=&q=([^&\ ]+)
RewriteRule ^ /q_%1? [L,NE,R=301]
RewriteRule ^q_(.*)$ /index.php/catalogsearch/result/?q=$1 [QSA,P,NC]
Is it possible? Thanks in advance
Just put the two rules one after the other and instead of using .*, use .+. Also, you don't need the P flag, instead use the L flag to end rewriting:
RewriteCond %{THE_REQUEST} \ /+catalogsearch/result/\?cat=(.+)&q=([^&\ ]+)
RewriteRule ^ /q_%2_cat_%1? [L,NE,R=301]
RewriteRule ^q_(.+)_cat_(.+)$ /index.php/catalogsearch/result/?q=$1&cat=$2 [QSA,L,NC]
RewriteCond %{THE_REQUEST} \ /+catalogsearch/result/\?cat=&q=([^&\ ]+)
RewriteRule ^ /q_%1? [L,NE,R=301]
RewriteRule ^q_([^_]+)$ /index.php/catalogsearch/result/?q=$1 [QSA,L,NC]
Related
#anubhava and #RavinderSingh13 have continued to help me learn and progress to where I am today with this script. This is what I have so far:
##1st rule with one parameter: $id--
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{QUERY_STRING} id=([0-9]+) [NC]
RewriteRule ^file\.php$ /directory/%1? [R=301,L,NC]
RewriteRule ^directory/(\d+)/?$ /directory/file.php?id=$1 [L,QSA,NC]
##Parameters are $id and $class
RewriteCond %{THE_REQUEST} \s/file\.php\?id=(\d+)&class=(\w+-class)\s [NC]
RewriteRule ^ /directory/%1/%2? [R=301,L]
RewriteRule ^directory/(\d+)/(\w+-class)/?$ /directory/file.php?id=$1&class=$2 [NC,L,QSA]
##Rule with two parameters: $id and $name--
RewriteCond %{THE_REQUEST} \s/file\.php\?id=(\d+)&name=(\S+)\s [NC]
RewriteRule ^ /directory/%1/%2? [R=301,L]
RewriteRule ^directory/(\d+)/(.*)/?$ /directory/file.php?id=$1&name=$2 [NC,L,QSA]```
Now, I would like to add one more rule with only one parameter instead of two. However, this parameter uses PHP $_GET['name'] in the url. I am trying to add one more rule that rewrites http://IPADDRESS.com/file.php?name=JohnDoe to http://IPADDRESS.com/newdirectory/JOHNDOE.
Thank you!
With your shown samples please try following. Please make sure to clear browser cache before testing your URLs.
RewriteEngine ON
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{QUERY_STRING} id=([0-9]+) [NC]
RewriteRule ^file\.php$ /directory/%1? [R=301,L,NC]
RewriteRule ^directory/(\d+)/?$ /directory/file.php?id=$1 [L,QSA,NC]
##Parameters are $id and $class
RewriteCond %{THE_REQUEST} \s/file\.php\?id=(\d+)&class=(\w+-class)\s [NC]
RewriteRule ^ /directory/%1/%2? [R=301,L]
RewriteRule ^directory/(\d+)/(\w+-class)/?$ /directory/file.php?id=$1&class=$2 [NC,L,QSA]
##Rule with two parameters: $id and $name--
RewriteCond %{THE_REQUEST} \s/file\.php\?id=(\d+)&name=(\S+)\s [NC]
RewriteRule ^ /directory/%1/%2? [R=301,L]
RewriteRule ^directory/(\d+)/(.*)/?$ /directory/file.php?id=$1&name=$2 [NC,L,QSA]
##Rule with two parameters: $name--
RewriteCond %{THE_REQUEST} \s/file\.php\?name=(\S+)\s [NC]
RewriteRule ^ /directory/%1? [R=301,L]
RewriteRule ^directory/([^/]*)/?$ /directory/file.php?name=$1 [NC,L,QSA]
# case: leading and trailing parameters
RewriteCond %{QUERY_STRING} ^(.+)?session=[0-9a-z]+&(.+)$ [NC]
RewriteRule (.*) /$1?%1&%2 [R=301,L]
# case: leading-only, trailing-only or no additional parameters
RewriteCond %{QUERY_STRING} ^(.+)?session=[0-9a-z]+$|^osCsid=[0-9a-z]+&?(.*)$ [NC]
RewriteRule (.*) /$1?%1 [R=301,L]
URL is:
https://www.test.com/test/?session=123
Shouldn't this cut off the ?session=123?
If not, how can I achieve this?
You can use this rule to remove a parameter from anywhere in the query string:
RewriteCond %{QUERY_STRING} ^(.*&)?session=[^&]*(?:&(.*))?$ [NC]
RewriteRule ^ %{REQUEST_URI}?%1%2 [L,R=301,NE]
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]
please help me with modifications .htaccess. How add ending character [/] in url.
Now .htaccess doing
www.mysite.cz/about.php > www.mysite.cz/about
www.mysite.cz/about/ > www.mysite.cz/about
i need this
www.mysite.cz/about.php > www.mysite.cz/about/
www.mysite.cz/about > www.mysite.cz/about/
RewriteEngine on
RewriteCond %{HTTP_HOST} ^mysite\.cz$
RewriteRule (.*) http://www.mysite.cz/$1 [R=301,L]
RewriteRule ^index.php$ http://mysite.cz/$1 [R=301,L]
RewriteCond %{THE_REQUEST} \ /+(.+)(?:/|\.php)(?:\?|\ )
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ /%1 [L,R=301]
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^([^.]+)$ $1.php [L]
Try this :
RewriteEngine on
RewriteCond %{HTTP_HOST} ^mysite\.cz$
RewriteRule (.*) http://www.mysite.cz/$1 [R=301,L]
RewriteRule ^(index.php)$ http://mysite.cz/$1 [NC,R=301,L]
RewriteCond %{THE_REQUEST} \ /+(.+)(?:/|\.php)(?:\?|\ )
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ /%1/ [NC,L,R=301]
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^([^.]+)/?$ $1.php [NC,L]
This adds a trailing slash at the end of the path :
www.mydomain.cz/about.php
would redirect to
www.mydomain.cz/about/
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/