I'm trying to add one last improvement (regarding htaccess), and that is that I would like it to redirect /?mod=inicio to /inicio. So I'm trying to do it with the following code, but It keeps building the url like this /inicio?mod=inicio
RewriteCond %{QUERY_STRING} ^mod=([a-z]+)$ [NC]
RewriteRule .* /%1 [L]
Same thing with extra parameters: from /?mod=x&type=y-z to /x/y-z
Try these rules instead:
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /\?mod=([a-z]+)&type=([a-z\-]+)
RewriteRule ^ /%1/%2? [L]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /\?mod=([a-z]+)($|\ )
RewriteRule ^ /%1? [L]
The key is to match against the actual request instead of the URI because your other rules (from your previous question) will cause these rewrites to match, they'll conflict with each other. The other key point is to include a ? at the end of the targets (e.g. /%1/%2?) which makes it so the query string doesn't get appended.
Related
I'm using Helicon Ape on a Windows Server to create htaccess files.
Originally, part of a larger set of conditions, I had this condition set to return 403 if the url contained (). However it is causing false positives in case of mailchimp tracking codes that end up getting wrapped in ()
RewriteCond %{QUERY_STRING} ^.*(\[|\]|\(|\)|<|>).* [NC,OR]
For example in the below URL
http://domain.com/page/11/page-name?ct=t(Newsletter_Tracking)
As an alternative, I was attempting to remove the parenthesis and redirect to a "cleaned version".
I tried a few different things that I found in SO but none worked.
So far the closest thing that I could get to working is this:
RewriteCond %{REQUEST_URI} [\(\)]+ [OR]
RewriteCond %{QUERY_STRING} [\(\)]+
RewriteRule ^(.*)[\(]+([^\)]*)[\)]+(.*)$ $1$2$3 [R=301,L]
The problem with the above code is that works if the () were in the URL but not the query string. It doesn't redirect and clean the querystring.
So this would work:
http://domain.com/page/11/pag(e-name)
but this wouldn't:
http://domain.com/page/11/page-name?ct=t(Newsletter_Tracking)
Your assistance is appreciated
Thank You.
You can use the following rule :
RewriteCond %{THE_REQUEST} /page/11/page-name\?ct=t\(Newsletter_Tracking\)\sHTTP [NC]
RewriteRule ^ %{REQUEST_URI}? [L,R]
If the querystring is dynamic, try:
RewriteCond %{THE_REQUEST} /page/11/page-name\?ct=.+\(.+\)\sHTTP [NC]
RewriteRule ^ %{REQUEST_URI}? [L,R]
Using #starkeen 's example, I was able to create a working solution.
This code handles the Query String separate from the URL. It cleans the URL but removes the query string.
RewriteCond %{REQUEST_URI} [\(\)]+
RewriteRule ^(.*)[\(]+([^\)]*)[\)]+(.*)$ $1$2$3 [R=301,L]
RewriteCond %{QUERY_STRING} [\(\)]+
RewriteRule ^ %{REQUEST_URI}? [R=301,L]
I have a URL structure like this
localhost/sumar/gallery.php?gen=slug-here
that i want to clean up for example: localhost/sumar/gallery/slug-here
so i need rewrite rule for this condition and prevent anyone from directly hitting the base URL http://localhost/sumar/gallery.php?gen=slug-here by auto redirecting it to localhost/sumar/gallery/slug-here
So far i have tried this method:
//that works
RewriteRule ^Gallery/([A-Za-z0-9-_]+)/?$ gallery.php?gen=$1
//below one does not seems to work
RewriteCond %{THE_REQUEST} ^GET\ /sumar/gallery\.php\?gen=([\w-]+) [NC]
RewriteRule ^sumar/gallery\.php$ /sumar/Gallery/%1? [R=301,L]
You'd need to act on the raw requests:
RewriteEngine On
RewriteRule ^(sumar)/Gallery/([\w-]+)/?$ /$1/gallery.php?gen=$1
RewriteCond %{THE_REQUEST} ^GET\ /sumar/gallery\.php\?gen=([\w-]+) [NC]
RewriteRule ^sumar/gallery\.php$ /sumar/Gallery/%1? [R=301,L]
PS: The character set [A-Za-z0-9_] can be represented with \w.
i want to rewrite
example.com/login?id=login&says=invalid&usr=username
request to
example.com/login&says=invalid&usr=username
and i am using this rules .
RewriteCond %{THE_REQUEST} \login?id=([^\ ]*)
RewriteRule ^$ /%1? [R,L]
but it says internal server error (500)
what is wrong with it? even the
RewriteCond %{THE_REQUEST} login\?id=([^\ ]*)
RewriteRule ^$ /%1? [R,L]
do not work .
i tried many thing and no result.
note that login file is not a real file but it have its rule that
example.com/login&says=invalid&usr=username
works fine.
**#loop :**
the page
example.com/login&says=invalid&usr=username
works fine but if system add another thing to it it will show 404
for example if system want to do the same thing again ( adding ?id=login&says=invalid&usr=username to end of address ) it shows 404
example.com/login&says=invalid&usr=username?id=login&says=invalid&usr=username
You can replace your with this rule:
RewriteCond %{THE_REQUEST} /(login[^?]*)\?id=[^&\s]+(&\S*)?\s [NC]
RewriteRule ^ /%1%2? [NE,R,L]
I am trying to make some really simple redirection from an old to a new website. But all my redirection with the character '&' dont work.
For exemple:
RewriteRule ^-Nos-equipes-$ /-Associes-Collaborateurs-?lang=fr [R=301,L]
work fine, but:
RewriteRule ^-Contact-?&lang=en$ /-Nous-contacter-?lang=en [R=301,L]
Dont work.
I am sure its easy, but i dont find anyway to make it work. I have try flag [NE] , or write [&]...
Thanks. Casp.
EDIT:
I now use the solution anubhava who works;
but not totaly cause my problem is more complexe;
In fact i got some url with param:
?&lang=en, ?&lang=fr, and ?lang=en
So i have had other query string like that:
# Redirection from the old site #--------------------------------------
RewriteCond %{QUERY_STRING} (^|&)lang=fr(&|$)
RewriteCond %{QUERY_STRING} (^|&)lang=en(&|$)
RewriteCond %{QUERY_STRING} ^lang=en(&|$)
This work still great; except in 2 cases:
the first one:
trying to make this redirection (stay on same url but juste modify the param)
RewriteRule ^the_url?&lang=en$ /the_url?lang=en [R=301]
who make an redirection error.
And second case when i have to rewrite the same url but with different param:
RewriteRule ^the_url$ /other_url?lang=fr [R=301]
RewriteRule ^the_url?&lang=en$ /other_url?lang=en [R=301]
RewriteRule ^the_url?&lang=fr$ /other_url?lang=fr [R=301,L]
It works but only with the first one insruction.
(i dont put the flag L, cause i got a long list of rewrite; i think i can put the condition at the beginning and not rewrite them each-time)
:::::::::::::::::::::::
Exemple of real url that i am trying to redirect who cause trouble
www.mywebsite/-Avocats-a-la-Cour- to--> www.mywebsite/?lang=fr
www.mywebsite/-Avocats-a-la-Cour-?&lang=en to--> www.mywebsite/?lang=en
www.mywebsite/Marque-francaise?&lang=fr to--> www.mywebsite//Marque-francaise?lang=fr
www.mywebsite/French-Trademarks?&lang=en to--> www.mywebsite//French-Trademarks?lang=en
www.mywebsite/-Contact-?&lang=en to--> www.mywebsite/-Nous-contacter-?lang=en
www.mywebsite/-Contact-?&lang=fr to--> www.mywebsite/-Nous-contacter-?lang=fr
www.mywebsite/person-780?lang=en to--> www.mywebsite/personel?lang=en
You cannot match QUERY_STRING in RewriteRule. You can use:
RewriteCond %{QUERY_STRING} (^|&)lang=en(&|$)
RewriteRule ^-?Contact-?$ /-Nous-contacter- [R=302,NC,L]
QUERY_STRING is automatically carried over to target URI.
EDIT: Based on your edited question you can have these rules:
# www.mywebsite/-Avocats-a-la-Cour- to--> www.mywebsite/?lang=fr
RewriteCond %{THE_REQUEST} \s/+(-Avocats-a-la-Cour-)\s [NC]
RewriteRule ^ /?lang=fr [R=302,L,NE]
# www.mywebsite/-Avocats-a-la-Cour-?&lang=en to--> www.mywebsite/?lang=en
RewriteCond %{THE_REQUEST} \s/+(-Avocats-a-la-Cour-)\?&?(lang=en)\s [NC]
RewriteRule ^ /?%2 [R=302,L,NE]
# www.mywebsite/Marque-francaise?&lang=fr to--> www.mywebsite/Marque-francaise?lang=fr
# www.mywebsite/French-Trademarks?&lang=en to--> www.mywebsite/French-Trademarks?lang=en
RewriteCond %{THE_REQUEST} \s/+(Marque-francaise|French-Trademarks)\?&(lang=(?:en|fr))\s [NC]
RewriteRule ^ /%1?%2 [R=302,L,NE]
# www.mywebsite/-Contact-?&lang=en to--> www.mywebsite/-Nous-contacter-?lang=en
# www.mywebsite/-Contact-?&lang=fr to--> www.mywebsite/-Nous-contacter-?lang=fr
RewriteCond %{THE_REQUEST} \s/+(-Contact-)\?&?(lang=(?:en|fr))\s [NC]
RewriteRule ^ /-Nous-contacter-?%2 [R=302,L,NE]
# www.mywebsite/person-780?lang=en to--> www.mywebsite/personel?lang=en
RewriteCond %{THE_REQUEST} \s/+(person-780)\?&?(lang=(?:en|fr))\s [NC]
RewriteRule ^ /personel?%2 [R=302,L,NE]
This doesnt look right
RewriteRule ^-Contact-?&lang=en$ /-Nous-contacter-?lang=en [R=301,L]
I would think the & should only be used when you have more than one parameter being passed
e.g.
?lang=en&userId=13
I would try:
RewriteRule ^-Contact-?lang=en$ /-Nous-contacter-?lang=en [R=301,L]
I have looked around quite a bit and tried myself and could not sort it out.
This is the URL before applying rewrite :
http://example.com/job-search/?searchText=GOOGLE+INC.&searchCity=Enter+US+City+or+Zipcode&searchYear=14&action=search&searchJobTitle=Enter+Job+Title+%2F+Role+Name
This is the URL after below rewrite rules are applied :
http://example.com/job-search/GOOGLE-INC./Enter-US-City-or-Zipcode/14/search/Enter-Job-Title
I have the below rules in .htaccess. It works well, when I have the URL like above with all the fields.
RewriteEngine On
RewriteBase /job-search/
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+(.*?)(?:\+|%20|\s)+(.+?)\sHTTP [NC]
RewriteRule ^ /%1-%2 [L,NE,R]
RewriteCond %{THE_REQUEST} \?searchText=([^\s&]+)&searchCity=([^\s&]+)&searchYear=(\d+)&action=([^\s&]+)&searchJobTitle=([^\s&]+) [NC]
RewriteRule ^ %1/%2/%3/%4/%5? [R=302,L,NE]
RewriteRule ^([^/]+)/([^/]+)/(\d+)/([^/]+)/([^/]+)/?$ ?searchText=$1&searchCity=$2&searchYear=$3&action=$4&searchJobTitle=$5 [NC,L,QSA]
The rewrite fails, when It gets empty values. The rules do not work and I get the below URL.
http://example.com/job-search/?searchText=GOOGLE-INC.&searchCity=&searchYear=14&action=search&searchJobTitle=
Of the three parameters ( searchText or searchCity or SearchJobTitle ), at a time, only one of the parameters will be having a value and other two will have blank values. Also, just want to ensure, when the user types the home page URL like http://example.com/job-search/ , it should not fail ( I tried something and it failed ) . Would like to avoid double slashes as well, when they are empty.
Below is the output I would like to see :
E.g when searchCity and SearchJobTitle are empty, the output URL should look like
http://example.com/job-search/GOOGLE-INC./14/search
When searchText and searchCity are empty and searchJobTitle value is 'Applicaton-Engineer', the output URL should look like
http://example.com/job-search/14/search/Application-Engineer
Can you please help me get the right rule ? Appreciate your help !
Change your rules to this:
RewriteEngine On
RewriteBase /job-search/
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+(.*?)(?:\+|%20|\s)+(.+?)\sHTTP [NC]
RewriteRule ^ /%1-%2 [L,NE,R]
RewriteCond %{THE_REQUEST} \?searchText=([^\s&]+)&searchCity=&searchYear=(\d+)&action=([^\s&]+)&searchJobTitle=\s [NC]
RewriteRule ^ %1/%2/%3/? [R=302,L,NE]
RewriteCond %{THE_REQUEST} \?searchText=([^\s&]+)&searchCity=([^\s&]+)&searchYear=(\d+)&action=([^\s&]+)&searchJobTitle=([^\s&]+) [NC]
RewriteRule ^ %1/%2/%3/%4/%5? [R=302,L,NE]
RewriteRule ^([^/]+)/([^/]+)/(\d+)/([^/]+)/([^/]+)/?$ ?searchText=$1&searchCity=$2&searchYear=$3&action=$4&searchJobTitle=$5 [NC,L,QSA]
RewriteRule ^([^/]+)/(\d+)/([^/]+)/?$ ?searchText=$1&searchYear=$2&action=$3 [NC,L,QSA]