Here is an example of my url rewrites.
# Redirect /page1.php?parameter-view=value to /page1/value
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+page1\.php\?parameter-view=([^\s]+) [NC]
RewriteRule ^ /page1/%1? [R=302,L]
# Internally forward /page1/value to /page1.php?parameter-view=value
RewriteRule ^page1/(.*)/(.*)/?$ /page1.php?parameter-view=$1¶meter-id=$2 [L,QSA,NC]
# Internally forward /page1/value to /page1.php?parameter-view=value
RewriteRule ^page1/(.*)?$ /page1.php?parameter-view=$1 [L,QSA,NC]
# Redirect /page2.php?parameter-view=value to /page2/value
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+page2\.php\?parameter-view=([^\s]+) [NC]
RewriteRule ^ /page2/%1? [R=302,L]
# Internally forward /page2/value to /page2.php?parameter-view=value
RewriteRule ^page2/(.*)/(.*)/?$ /page2.php?parameter-view=$1¶meter-id=$2 [L,QSA,NC]
# Internally forward /page2/value to /page2.php?parameter-view=value
RewriteRule ^page2/(.*)?$ /page2.php?parameter-view=$1 [L,QSA,NC]
I have a set of rewrites for page1 and page2. Is there a way to combine these rewrites so everytime I make a new page I can just add the page "page3" instead of adding a 3rd group of rewrites? Example of what I'm going for:
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+(page1|page2|page3)\.php\?parameter-view=([^\s]+) [NC]
RewriteRule ^ /(page1|page2|page3)/%1? [R=302,L]
RewriteRule ^(page1|page2|page3)/(.*)/(.*)/?$ /(page1|page2|page3).php?parameter-view=$1¶meter-id=$2 [L,QSA,NC]
RewriteRule ^(page1|page2|page3)/(.*)?$ /(page1|page2|page3).php?parameter-view=$1 [L,QSA,NC]
The best case scenario is i refine not only the idea of using the same rewrite for all pages but maybe not even having to use 3 to do the job?
Yes sure you can do something like this:
RewriteCond %{THE_REQUEST} \s/+(page1|page2|page3)\.php\?parameter-view=([^\s]+) [NC]
RewriteRule ^ /%1/%2? [R=302,L]
RewriteRule ^(page1|page2|page3)/(.*)/(.*)/?$ /$1.php?parameter-view=$2¶meter-id=$3 [L,QSA,NC]
RewriteRule ^(page1|page2|page3)/(.*)/?$ /$1.php?parameter-view=$2 [L,QSA,NC]
Related
Im trying to redirect several urls with 3 parameters to different static urls with .htaccess but nothing working.
1.
http://olddomain.com/index.php?id_category=28&controller=category&id_lang=2
to
https://newdomain.com/page1/
http://olddomain.com/index.php?id_category=30&controller=category&id_lang=2
to
https://newdomain.com/page2/
http://olddomain.com/index.php
to
https://newdomain.com
I tried the below code but http://olddomain.com/index.php not going to https://newdomain.com :
RewriteCond %{HTTP_HOST} ^olddomain.com [NC,OR]
RewriteCond %{HTTP_HOST} ^www.olddomain.com [NC]
RewriteRule ^(.*)$ https://newdomain.com/$1 [L,R=301,NC]
RewriteCond %{QUERY_STRING} ^id_category=28&controller=category&id_lang=2$
RewriteRule ^index.php$ https://newdomain.com/page1/? [R=301,L]
RewriteCond %{QUERY_STRING} ^id_category=30&controller=category&id_lang=2$
RewriteRule ^index.php$ https://newdomain.com/page2/? [R=301,L]
You need to have specific longer matches first and then have rules to remove index.php or domain redirect:
RewriteEngine On
# specific redirects with index.php as optional match
RewriteCond %{QUERY_STRING} ^id_category=28&controller=category&id_lang=2$ [NC]
RewriteRule ^(index\.php)?$ https://newdomain.com/page1/? [R=301,L,NC]
RewriteCond %{QUERY_STRING} ^id_category=30&controller=category&id_lang=2$ [NC]
RewriteRule ^(index\.php)?$ https://newdomain.com/page2/? [R=301,L,NC]
# remove index.php and redirect to newdmain
RewriteCond %{HTTP_HOST} ^(?:www\.)?olddomain\.com$ [NC]
RewriteRule ^(?:index\.php/?)?(.*)$ https://newdomain.com/$1 [L,R=301,NC,NE]
Make sure to clear your browser cache before testing this change.
In case you are taking page's id from id_lang= variable then please try following rules. Please make sure to clear your browser cache before testing your URLs.
RewriteEngine ON
##Rules to redirect to link: https://newdomain.com/page1/ here.
RewriteCond %{HTTP_HOST} ^(?:www\.)?olddomain\.com$ [NC]
RewriteCond %{THE_REQUEST} \s/index\.php\?id_category=28&controller=category&id_lang=(\d+)\s [NC]
RewriteRule ^ https://newdomain.com/page%1/? [NE,R=301,L]
##Rules to redirect https://newdomain.com/ here.
RewriteCond %{HTTP_HOST} ^(?:www\.)?olddomain\.com$ [NC]
RewriteCond %{THE_REQUEST} \s/index\.php\s [NC]
RewriteRule ^ https://newdomain.com [NE,R=301,L]
I think that this is rather simple, but I just can't wrap my head around it.
I have a domain like https://www.example.com
I want to rewrite it to https://example.com
That works. But I am also rewriting other urls like https://www.example.com/privacy
If I try to rewrite these too the user is always redirected to https://example.com/privacy.php
How do I prevent my server to show the actual filename instead of its rewritten url?
My code looks like this:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\. [NC,OR]
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^ https://%1%{REQUEST_URI} [R=301,L,NE]
RewriteRule ^checkout checkout.php [L]
RewriteRule ^imprint imprint.php [L]
RewriteRule ^privacy privacy.php [L]
RewriteRule ^terms terms.php [L]
RewriteRule ^allergy allergy.php [L]
RewriteRule ^nutrition nutrition.php [L]
RewriteRule ^order/([A-Z]*)?$ confirmation.php?lang=&id=$1 [L,QSA]
Any help is appreciated.
Regards
Have it this way:
Options -MultiViews
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\. [NC,OR]
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^ https://%1%{REQUEST_URI} [R=301,L,NE]
RewriteRule ^(checkout|imprint|privacy|terms|allergy|nutrition)/?$ $1.php [L]
RewriteRule ^order/([a-z]+)/?$ confirmation.php?lang=&id=$1 [L,QSA,NC]
Make sure to test after clearing your browser cache. Problem appears to be due to your patterns not using end anchor.
I have combined your multiple similar rules into one rule.
I have some URLs which i want to rewrite and redirect
mysite.com/search2.php?search=abc should be redirected to mysite.com/search/abc.
And mysite.com/search/abcshould show page mysite.com/search2.php?search=abc without changing URL.
I have written the rewrite code but can't do redirection without causing an infinite loop.
RewriteEngine On
RewriteRule ^search/([^/]+)?$ search2.php?search=$1 [QSA,NC]
RewriteRule ^search/([^/]+)/([^/]+)/([^/]+)?$ search2.php?search=$1&$2=$3 [L,QSA,NC]
RewriteRule ^search/([^/]+)/([^/]+)/([^/]+)/([^/]+)/([^/]+)?$ search2.php?search=$1&$2=$3&$4=$5 [L,QSA,NC]
RewriteRule ^search/([^/]+)/([^/]+)/([^/]+)/([^/]+)/([^/]+)/([^/]+)/([^/]+)?$ search2.php?search=$1&$2=$3&$4=$5&$6=$7 [L,QSA,NC]
RewriteRule ^file/([^/]+)/([^/]+)/([^/]+)/([^/]+)?$ file2.php?$1=$2&$3=$4 [L,QSA,NC]
RewriteRule ^directory/([^/]+)/([^/]+)/([^/]+)/([^/]+)?$ directory2.php?$1=$2&$3=$4 [QSA,NC]
RewriteRule ^directory/([^/]+)/([^/]+)/([^/]+)/([^/]+)/([^/]+)/([^/]+)?$ directory2.php?$1=$2&$3=$4&$5=$6 [L,QSA,NC]
You can use this htaccess
RewriteEngine on
RewriteCond %{THE_REQUEST} /search\.php\?search=([^\s]+)\s [NC]
RewriteRule ^ /search/%1? [L,R]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^./]+)/?$ /search.php?search=$1 [L]
I have changed my web page url from localhost/products.php to localhost/products using the code below.
RewriteCond %{THE_REQUEST} /products\.php[\s/?] [NC]
RewriteRule ^ /products [R=301,L]
RewriteRule ^products/?$ products.php [L,NC]
But when I navigate to next page, address bar shows something like:
localhost/products?page=2 which I want to appear as localhost/products/page2
Tried this RewriteRule ^products/page/([0-9]+)/?$ products.php?page=$1 [L] but this did not work.
My full code for rewriting page url is:
RewriteCond %{THE_REQUEST} /products\.php[\s/?] [NC]
RewriteRule ^ /products [R=301,L]
RewriteRule ^products/?$ products.php [L,NC]
RewriteRule ^products/page/([0-9]+)/?$ products.php?page=$1 [L]
Here how can I change the URL for next pages? I mean from localhost/products?page=2 to localhost/products/page2 and so on.
You can use the following rule :
RewriteCond %{THE_REQUEST} /products/?(?:\.php)?\?page=([^\s&]+) [NC]
RewriteRule ^ /products/%1? [R=301,L]
RewriteRule ^products/([0-9]+)/?$ /products.php?page=$1 [L,NC]
This will convert the urls
/products.php?page=number
/products/?page=number
to
/products/number
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]