When a user accesses test.domain.co.uk, I would like to show them the page at channels/index.php?url=test where test can be anything.
I have this working with the following code:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.domain\.co\.uk$
RewriteCond %{HTTP_HOST} ^.+\.domain\.co\.uk$
RewriteRule ^(.*) %{HTTP_HOST}$1 [C]
RewriteRule ^(.+)\.domain\.co\.uk/? channels/index.php?url=$1 [L]
However, I would also like it to work if you called http://test.domain.co.uk/login/
And it would show you channels/index.php?url=test&page=login
I have tried a few ways and it just shows it as if it is ?url=test
Add
RewriteRule ^(.+)\.domain\.co\.uk(.+)$ channels/index.php?url=$1&page=$2 [L]
to the end of your rules. And add a $ to the pattern of your last rule:
RewriteRule ^(.+)\.domain\.co\.uk/?$ channels/index.php?url=$1 [L]
Then to keep from looping, add this to the top:
RewriteCond %{ENV:REDIRECT_STATUS} 200
RewriteRule ^ - [L]
You can use this rule:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.
RewriteCond %{HTTP_HOST} ^([^.]+)\.domain\.co\.uk$
RewriteRule ^((?!channels/index\.php).*)$ /channels/index.php?url=%1&page=$1 [L,QSA]
Related
i'm new on htaccess. I'm working on a website with multiple query strings.
I need to rewrite urls of different query strings like:
/path_to_website/file.php?product=var1&country=0&pag=1 to /path_to_website/var1/
/path_to_website/file.php?product=var1&country=0&pag=var2 to /path_to_website/var1/var2
/path_to_website/file.php?product=var1&country=var2&pag=1 to /path_to_website/var1/var2/
/path_to_website/file.php?product=var1&country=var2&pag=var3 to /path_to_website/var1/var2/var3
As you can see, the problem is between 2 and 3 rewrite because they have the same number of parameters (if the problem is this). I've done this on htaccess file:
###BLOCK 1#######
RewriteCond %{QUERY_STRING} ^$
RewriteRule ^([^/]+)/$ /path_to_website/modules/file.php?product=$1&country=0&pag=1&rew=1 [L]
RewriteCond %{QUERY_STRING} ^(.*)=(.*)&country=0&pag=1$
RewriteCond %{QUERY_STRING} !^.*rew=1.*$
RewriteRule ^.*$ /path_to_website/%2/? [R=301,L]
###BLOCK 2#######
RewriteCond %{QUERY_STRING} ^$
RewriteRule ^([^/]+)/([^/]+)/$ /path_to_website/modules/file.php?product=$1&country=0&pag=$2&rew=1 [L]
RewriteCond %{QUERY_STRING} ^(.*)=(.*)&country=0&(.*)=(.*)$
RewriteCond %{QUERY_STRING} !^.*rew=1.*$
RewriteRule ^.*$ /path_to_website/%2/%4/? [R=301,L]
###BLOCK 3#######
RewriteCond %{QUERY_STRING} ^$
RewriteRule ^([^/]+)/([^/]+)/$ /path_to_website/modules/file.php?product=$1&country=$2&pag=1&rew=1 [L]
RewriteCond %{QUERY_STRING} ^(.*)=(.*)&(.*)=(.*)&pag=1$
RewriteCond %{QUERY_STRING} !^.*rew=1.*$
RewriteRule ^.*$ /path_to_website/%2/%4/? [R=301,L]
###BLOCK 4#######
RewriteCond %{QUERY_STRING} ^$
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/$ /path_to_website/modules/file.php?product=$1&country=$2&pag=$3&rew=1 [L]
RewriteCond %{QUERY_STRING} ^(.*)=(.*)&(.*)=(.*)&(.*)=(.*)$
RewriteCond %{QUERY_STRING} !^.*rew=1.*$
RewriteRule ^.*$ /path_to_website/%2/%4/%6/? [R=301,L]
How can I fix this? Because everything works except when I search for product and country !=0 the engine stops on block 2.
Please someone help me because I'm stuck on this problem.
Thanks a lot!
Edit
Thanks #RavinderSingh13 for your answer. I tried with your rules, like this:
RewriteCond %{QUERY_STRING} ^$
############your rule###########
RewriteRule ^([\w-]+)/?$ file.php?product=$1&country=0&pag=1 [L]
RewriteCond %{QUERY_STRING} ^(.*)=(.*)&country=0&pag=1$
RewriteRule ^.*$ /path_to_website/%2/? [R=301,L]
The rewrite with this rules is path_to_website/product/, but the page doesn't work with "ERR_TOO_MANY_REDIRECTS". I tried also with ONLY your rule:
RewriteRule ^([\w-]+)/?$ file.php?product=$1&country=0&pag=1 [L]
In this case, the rewrite don't work at all, just print the query string. I also tried with the rew=1 key, like this:
RewriteCond %{QUERY_STRING} ^$
RewriteRule ^([\w-]+)/?$ /aziende_agrarie/modules/risultato_ricerca.php?
prodotto=$1&provincia=0&pag=1&rew=1 [L]
RewriteCond %{QUERY_STRING} ^(.*)=(.*)&provincia=0&pag=1$
RewriteCond %{QUERY_STRING} !^.*rew=1.*$
RewriteRule ^.*$ /aziende_agrarie/%2/? [R=301,L]
The rewrite works with path_to_website/product, but when i change the page on 2, the browser url is path_to_website/product/?product=var1&country=0&pag=2. So I insert the rewrite for product/2/, and the rewrite for "file.php?product=var1&country=0&pag=var2" is:
RewriteCond %{QUERY_STRING} ^$
RewriteRule ^([\w-]+)/([\w-]+)(?!=/)$ /aziende_agrarie/modules/risultato_ricerca.php?
prodotto=$1&provincia=0&pag=$2&rew=1 [L]
RewriteCond %{QUERY_STRING} ^(.*)=(.*)&provincia=0&(.*)=(.*)$
RewriteCond %{QUERY_STRING} !^.*rew=1.*$
RewriteRule ^.*$ /aziende_agrarie/%2/%4/? [R=301,L]
An so on. I miss something? Thanks a lot for your help :)
Based on your shown samples, could you please try following Rule sets in your .htaccess file. Also please make sure you clear your cache before testing your URLs.
RewriteEngine ON
##For file.php?product=var1&country=0&pag=1
RewriteRule ^([\w-]+)/?$ file.php?product=$1&country=0&pag=1 [L]
##For file.php?product=var1&country=0&pag=var2
RewriteRule ^([\w-]+)/([\w-]+)(?!=/)$ file.php?product=$1&country=0&pag=$2 [L]
##For file.php?product=var1&country=var2&pag=1
RewriteRule ^([\w-]+)/([\w-]+)/$ file.php?product=$1&country=$2&pag=1 [L]
##for file.php?product=var1&country=var2&pag=var3
RewriteRule ^([\w-]+)/([\w-]+)/([\w-]+)$ file.php?product=$1&country=$2&pag=$3 [L]
Edit
I had a GET request with form submit and href with custom paginator. I solved the problem. The problem was on href because i left the query string like "path_to_website/?product=var1&country=var2&page=1" etc. I resolved this replacing the query string href with "/product/country" for pag1 and so on for the other href. On the submit event, i left my custom block 1 and block 3 rules and for paginator href i wrote your rules for rewrite.
Here is my htaccess code:
RewriteEngine On
RewriteRule ^s/(.*)/(.*) /index.php?search=$1&category=$2 [L,QSA]
RewriteCond %{QUERY_STRING} ^search=([A-Za-z0-9\+]+)$
RewriteRule ^(.*)$ /s/%1/? [R=301,L]
RewriteCond %{QUERY_STRING} ^search=([A-Za-z0-9\+]+)&category=([A-Za-z0-9\+]+)$
RewriteRule ^(.*)$ /s/%1/%2/? [R=301,L]
I need to make a rewrite rule like this:
http://mywebsite.com/s/query+term => http://mywebsite.com/?search=query+term
OR if there is a category
http://mywebsite.com/s/query+term/Category => http://mywebsite.com/?search=query+term&category=Category
I need also to redirect all the old urls to the new one.
With this rules all that I can obtain is to have the search term and category name joined togheter. In a nutshell it is as if I had always:
http://mywebsite.com/?search=query+term/Category
If I remove all the conditions (RewriteCond), leaving only the rewrite rule with the addition of a trailing slash:
RewriteEngine On
RewriteRule ^s/(.*)/(.*)/ /index.php?search=$1&category=$2 [L,QSA]
Reaching the url of my interest in a direct way, the whole thing works. But I will not have the redirect..
So I have tried to set a rule like this:
RewriteEngine On
RewriteRule ^s/(.*)/(.*)/ /index.php?search=$1&category=$2 [L,QSA]
RewriteCond %{QUERY_STRING} ^search=([A-Za-z0-9\+]+)$
RewriteRule ^(.*)$ /s/%1/? [R=301,L]
RewriteCond %{QUERY_STRING} ^search=([A-Za-z0-9\+]+)&category=([A-Za-z0-9\+]+)$
RewriteRule ^(.*)$ /s/%1/%2/? [R=301,L]
But with this rule I get just a loop of redirects.
You can use the following rules :
RewriteEngine on
#1--Redirect from "?search=foobar&cat=cat" to "/s/foobar/cat" --#
RewriteCond %{THE_REQUEST} /\?search=([^&]+)&cat=([^\s]+) [NC]
RewriteRule ^ /s/%1/%/%2? [NC,L,R]
#2--Redirect from "/?search=foobar" to "/s/foobar" --#
RewriteCond %{THE_REQUEST} /\?search=([^/\s]+) [NC]
RewriteRule ^ /s/%1? [NC,L,R]
#1--Rewrite "s/foobar/cat" to "/?search=foobar&cat=cat"--#
RewriteRule ^s/([^/]+)/([^/]+)/?$ /?search=$1&$category=$2 [NC,L]
#2--Rewrite "s/foobar" to "/?search=foobar"--#
RewriteRule ^s/([^/]+)/?$ /?search=$1 [NC,L]
i have this link
http://localhost/fukhtaccess
and i want redirect the link to http://server.localhost/index.php with GET params.
this is my htaccess, but don't work and not possibile test with some error for debug (thanks apache foundation!!!)
RewriteEngine On
RewriteCond %{HTT_HOST} ^localhost$
RewriteCond %{REQUEST_URI} fukhtaccess
RewriteRule (.*) http://server.localhost/index.php [QSA,L]
You have a typo: change HTT_HOST to HTTP_HOST.
Also, your second RewriteCond just complicates things unnecessarily. Do this instead:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^localhost$
RewriteRule fukhtaccess http://server.localhost/index.php [QSA,L]
better, this work fine
RewriteEngine On
RewriteCond %{HTTP_HOST} ^localhost [NC]
RewriteCond %{REQUEST_URI} fukhtaccess$ [NC]
RewriteRule (.*) http://server.localhost/index.php [QSA,L]
I have this code to rewrite a path, and then redirect to subdomain's folder:
RewriteEngine On
RewriteBase /
RewriteRule ^category/?$ category.php
RewriteRule ^category/[a-z0-9\-]+\_([A-Z0-9\_]+)/?$ articles.php?id=$1§ion=category
RewriteCond %{HTTP_HOST} ^test.(.*)example.com/?$ [NC]
RewriteCond %{REQUEST_URI} !^/\!test/ [NC]
RewriteRule (.*) /!test/$1 [L,NC]
RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [NE,L,R=301]
The big problem is that typing this in the browser address field and press Enter:
http://test.example.com/category/article-about-something_35436
Ends up in that it changes by itself to:
http://test.example.comcategory/article-about-something_35436
And the browser of course tells you that "Server is not found".
Troubleshooting
I have figured out that this works fine:
http://test.example.com/category
And removing the query string ?id=$1§ion=category from the second rewrite rule works. You don't end up on the article of course but on articles.php.
So, anyone know the problem?
Rearrange your rules and keep redirect rules with R flag before other internal rewrite rules:
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [NE,L,R=301]
RewriteRule ^category/?$ category.php [L,NC]
RewriteRule ^category/[a-z0-9-]+_([A-Z0-9_]+)/?$ articles.php?id=$1§ion=category [L,QSA]
RewriteCond %{HTTP_HOST} ^test\.(.*)example\.com$ [NC]
RewriteCond %{REQUEST_URI} !^/\!test/ [NC]
RewriteRule (.*) /!test/$1 [L,NC]
I need to create a rule for all GET requests to my site so that:
GET /articles/topic1.html?showall=1
is rewritten as:
GET /articles/topic1.html
I have the following .htaccess file on my Joomla site:
RewriteEngine On
RewriteCond %{QUERY_STRING} mosConfig_[a-zA-Z_]{1,21}(=|%3D) [OR]
RewriteCond %{QUERY_STRING} base64_encode.*(.*) [OR]
RewriteCond %{QUERY_STRING} (<|%3C).*script.*(>|%3E) [NC,OR]
RewriteCond %{QUERY_STRING} GLOBALS(=|[|%[0-9A-Z]{0,2}) [OR]
RewriteCond %{QUERY_STRING} _REQUEST(=|[|%[0-9A-Z]{0,2})
RewriteRule ^(.*)$ index.php [F,L]
########## This is the Rule I have added ################
RewriteRule ^(.*)$ /$showall=1 [R=301,L]
RewriteBase /
I have added comments on the line that I have added. Unfortunately it doesn't work, the browser warns me I have generated a circular loop....
Any idea how to fix it ??
Thanks
Frank
Try replacing that with:
RewriteCond %{QUERY_STRING} ^showall=([0-9])+$
RewriteRule ^articles/topic[0-9]*\.html /articles/topic%1.html? [L]
Or if it's really topic1.html, then you want this:
RewriteCond %{QUERY_STRING} ^showall=1$
RewriteRule ^articles/topic1\.html /articles/topic1.html? [L]