Here's my code in htaccess:
RewriteCond %{THE_REQUEST} \s/user/\?user=(.*)\s [NC]
RewriteRule ^ /user/%1? [R=301,L]
RewriteRule ^user/(.*)$ /user/?user=$1 [L]
What I want it to do is take the URL /user/?user=username and rewrite it to /user/username
What I have successfully rewrites it to /user/username but then it gives me a 500 error. If anyone could tell me why I would be very appreciative.
Thanks!
Edit:
The 500 error seems to be because it's creating a redirect loop with this rule. I'm not sure how to take just the last part and append it to the URL as a query string.
Please change this line :
RewriteRule ^user/(.*)$ /user/?user=$1 [L]
with this :
RewriteRule ^user/[a-zA-Z]+$ /user/?user=$1 [L]
You should match only letters , otherwise the above condition RewriteCond %{THE_REQUEST} \s/user/\?user=(.*)\s [NC] doesn't make any sense with (.*)$
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 know with a simple redirect one (Sub-)Domain or Folder to another i can get rid of a string like this
RewriteEngine On
RewriteRule (.*) http://www.domain.tld/? [R=301,L]
I know how to get rid of it when it is a simple file too.
But when it comes to the Rootdomain itself (http://domain.tld/?Stringwhatsoever), i am at a loss here. My last try used a modified version of a redirect I used to redirect files and folders around and that worked pretty nicely and also removed the query, but it ended up in a redirection error.
RewriteRule ^ http://domain.tld/? [L,NC,R=301]
So i have no clue how to get rid of Query Strings at urls without breaking it.
Try this :
RewriteEngine On
RewriteCond %{THE_REQUEST} /\?([^\s]+) [NC]
RewriteRule ^$ http://domain.com/? [NC,R,L]
Or
RewriteEngine On
RewriteCond %{QUERY_STRING} (.+) [NC]
RewriteRule ^$ http://domain.com/? [NC,R,L]
Reference :
-https://wiki.apache.org/httpd/RewriteQueryString
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 got the following url:
127.0.0.1/abc_123456/default/index/index/
Which should be rewritten to:
127.0.0.1/123456/index.php/default/index/index/
So remove abc_ and add index.php after it. Problem is that the digits are variable, but the abc_ isn't.
I had the following rule:
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /abc_
RewriteRule ^abc_(.*)/(.*)$ /$1/index.php/$2
But that resulted in the url being rewritten to:
127.0.0.1/123456/default/index/index.php/index/
Seems like I'm almost there, but I can't figure it out.
Thanks in advance
Use this simple rule:
RewriteRule ^abc_([0-9]+)/(.*)$ $1/index.php/$2 [L,NC]
Try
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /abc_([0-9]+)/([^\ \?]+) [NC]
RewriteRule ^ /%1/index.php/%2 [L]
EDIT
However, you are right about the other rule, that's the one giving the error; RewriteCond %{THE_REQUEST} !^[A-Z]+\ /abc_ RewriteRule ^(.*)$ /index.php/$1 That one is used to rewrite if the page does not contain the /abc_123456/
Add an extra condition to that rule as below
#if not abc_
RewriteCond %{THE_REQUEST} !^[A-Z]+\ /abc_ [NC]
#if not already index.php
RewriteCond %{REQUEST_URI} !^/index\.php$ [NC]
RewriteRule ^(.*)$ /index.php/$1 [L]
for example, if you need similar solution, when visited this url:
http://yoursite.com/subpage1/subpage2/?YOURSTRING=blabla
to redirected visitor to
http://yoursite.com/subpage1/subpage2/
then see link - http://stackoverflow.com/a/15680832/2215124
I am trying to get Apache to redirect /a.php?a=123 to /b/123 (where 123 could be any number between 1 and 9999) but can't seem to get it to work.
This is what I have in htaccess:
RewriteEngine on
RewriteRule ^a.php?a=([0-9]+) /b/$1 [L]
RewriteRule ^a.php$ /c/ [L]
With this going to a.php?a=123 results in 404, but going to just a.php works as expected.
I tried escaping the ? (RewriteRule ^a.php\?a=([0-9]+) /b/$1 [L]) but it still doesn't work.
What am I doing wrong please?
The query string is not part of the URI path that is tested in the RewriteRule directive. This can only be tested with a RewriteCond directive:
RewriteCond %{QUERY_STRING} ^a=([0-9]+)$
RewriteRule ^a\.php$ /b/%1? [L,R]
RewriteCond %{QUERY_STRING} ^$
RewriteRule ^a\.php$ /c/ [L,R]
But if you want it the other way (requests of /b/123 are redirected to /a.php?a=123):
RewriteRule ^b/([0-9]+)$ a.php?a=$1 [L]