I am trying to rewrite my own shortcuts to full utm_ parameters using the following rules:
# rename query parameter m=>utm_medium
RewriteCond %{QUERY_STRING} ^(.*[&?])?m=([^&]*)(&.*)?$ [NC]
RewriteRule ^ %{REQUEST_URI}?%1utm_medium=%2%3 [NC,NE,L,R=301]
# rename query parameter s=>utm_source
RewriteCond %{QUERY_STRING} ^(.*[&?])?s=([^&]*)(&.*)?$ [NC]
RewriteRule ^ %{REQUEST_URI}?%1utm_source=%2%3 [NC,NE,L,R=301]
# rename query parameter c=>utm_campaign
RewriteCond %{QUERY_STRING} ^(.*[&?])?c=([^&]*)(&.*)?$ [NC]
RewriteRule ^ %{REQUEST_URI}?%1utm_campaign=%2%3 [NC,NE,L,R=301]
Given the following URL:
https://www.example.com/?m=value1&s=value2&c=value3
The m parameter never gets replaced. Location of this parameter in the URL (front, middle, end) does not matter and location of the rule in htaccess file does not matter as well. The rule never works.
But replacing the rule for parameter um (also: see pre-existing s or c) works like charm on https://www.example.com/?um=value1&s=value2&c=value3
# rename query parameter um=>utm_medium
RewriteCond %{QUERY_STRING} ^(.*[&?])?um=([^&]*)(&.*)?$ [NC]
RewriteRule ^ %{REQUEST_URI}?%1utm_medium=%2%3 [NC,NE,L,R=301]
Is there any restriction on usage of parameter m in mod_rewrite or am I missing something?
Related
I'm aware this has already been asked, but I've tried countless solutions but I'm still not getting the desired result.
I've a page which shows a list of names and the variables data are the starting letter (variable l which can also be a number) and the page number (page_no) and the page number is optional as it should not be displayed in the page 1.
The original URL is the following
https://www.example.com/allnames.php?l=A&page_no=1
I've the following rule
RewriteCond %{QUERY_STRING} ^l=([\w]+)$
RewriteRule ^allnames.php$ /allnames/%1? [R=301,L]
which works fine for page 1, but it doesn't include the page_no variable, so I tried this one but I'm unable to understand what's wrong.
RewriteCond %{QUERY_STRING} ^l=([\w]+)?page_no=([0-9]+)$
RewriteRule ^allmovies.php$ /allmovies/%1/%2? [R=301,L]
the result expected would be, for example
allnames/A or allnames/A/1 for letter A, page 1
allnames/S/8 for letter S, page 8
And the following is the content of the htaccess file
RewriteEngine On
Options +FollowSymLinks -MultiViews
# to prevent loops
RewriteCond %{ENV:REDIRECT_STATUS} !200
#remove index.php
RewriteCond %{REQUEST_URI}::$1 ^(/.+)/(.*)::\2$
RewriteRule ^(.*) - [E=BASE:%1]
# Sets the HTTP_AUTHORIZATION header removed by apache
RewriteCond %{HTTP:Authorization} .
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^index\.php(/(.*)|$) %{ENV:BASE}/$2 [R=301,L]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule .? - [L]
RewriteRule .? %{ENV:BASE}/index.php [L]
# allnames.php
RewriteCond %{QUERY_STRING} ^l=([\w]+)$
RewriteRule ^allnames.php$ /allnames/%1? [R=301,L]
RewriteRule allnames/ /allnames.php?l=$1&page_no=$2 [L]
RewriteCond %{QUERY_STRING} ^l=([\w]+)?page_no=([0-9]+)$
The regex in the condition (CondPattern) does not allow for the & delimiter between URL parameters. You are also allowing the l parameter value to be entirely omitted (which would result in an ambiguous URL) or contain multiple characters (which you've stated should be a single "letter").
You would seem to need something like the following instead:
RewriteCond %{QUERY_STRING} ^l=([\w])&page_no=([0-9]+)$
But note that this will fail if the l parameter value contains more than one character (or is omitted entirely).
the result expected would be, for example
allnames/A or allnames/A/1 for letter A, page 1
Note that you would still need your first rule (without the page_no URL param) to get allnames/A. You could instead make page_no optional in the second rule, but you would still get a trailing slash (ie. allnames/A/) if that is an issue?
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]
.htaccess and regular expressions is twisting my mind :)
I'm trying to catch a variable number of variables from the query string, write them in a cookie and redirect removing query string. I got it working with only one variable but sometimes I need it to catch 2 or 3. Here's the code that works with one variable:
RewriteCond %{QUERY_STRING} ^(tag|aid|flu)=([a-z0-9]+)$ [NC] # look for interesting variables in url
RewriteRule ^(.*)$ $1? [CO=%1:%2:foo.com:14400:/,R=301,L] # capture url strip for vars, write cookie, redir
but clearly this is not going to work if the requested url is foo.com?tag=xx&aid=yyy&flu=bbb or with just two variables. I just can't seem to wrap my head around how to do this. Also it would be nice if the order of the variables didn't matter.
Thanks!
I don't know if it's possible to do that:
# tag
RewriteCond %{QUERY_STRING} (?:^|&)tag=([a-z0-9]+)(?:&|$) [NC]
RewriteRule ^ - [CO=tag:%1:foo.com:14400:/]
# aid
RewriteCond %{QUERY_STRING} (?:^|&)aid=([a-z0-9]+)(?:&|$) [NC]
RewriteRule ^ - [CO=aid:%1:foo.com:14400:/]
# flu
RewriteCond %{QUERY_STRING} (?:^|&)flu=([a-z0-9]+)(?:&|$) [NC]
RewriteRule ^ - [CO=flu:%1:foo.com:14400:/]
# Final
RewriteCond %{QUERY_STRING} (?:tag|aid|flu)=.+ [NC]
RewriteRule ^(.*)$ $1? [R=301,L]
How can i replace + to - in my url. What code shoud I add in my htacces to get rid of + and replace it with a minus.
RewriteCond %{QUERY_STRING} ^search=([^&]+)$
RewriteRule ^ http://mysite.com\/Download\/free\/%2.html? [R,L,NE]
If i type a search that contain spaces, every space become + and i want -.
I'm assuming that the bla+bla+bla part of the URL: http://mysite.com/Download/free/bla+bla+bla.html originated from the query string search='s value. Depending on what other rules you may have, you can go about this in 2 different ways. You can either remove all of the spaces from the query string first, before it's redirected to the .html file. Or you can rewrite the query string into the URI, then remove the spaces before redirecting. It'll be something like this:
RewriteCond %{QUERY_STRING} ^search=(.*?)(\+|%20)(.*)$
RewriteRule ^ /?search=%1-%3 [L,NE]
RewriteCond %{QUERY_STRING} !(\+|%20)
RewriteCond %{QUERY_STRING} ^search=([^&]+)$
RewriteRule ^ http://mysite.com\/Download\/free\/%1.html? [R,L,NE]
Note that in your htaccess you have the %2 back reference, which doesn't seem to reference anything.
Or rewrite to URI first, then redirect:
RewriteCond %{QUERY_STRING} ^search=([^&]+)$
RewriteRule ^ /Download\/free\/%1.html? [L,NE]
RewriteCond %{REQUEST_URI} !(\ )
RewriteRule ^ - [L,R]
RewriteRule ^(.*)\ (.*)$ /$1-$2 [L]
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.