After a couple of hours of fighting, I manage to generate an htacces file to redirect all my old URLs to the URLs of my new site (same domain). I am using the following rules to manage my subfolders redirects (my new site has only a couple of pages):
RewriteRule ^es/empresa/terminos-y-condiciones.html$ http://domain/terminos-y-condiciones.html [L]
RewriteRule ^es/servicios/registro-de-dominios/.*$ http://domain/dominios.html [R=301,NC,L]
RewriteRule ^es/empresa/.*$ http://domain/nosotros.html [R=301,NC,L]
RewriteRule ^es/component/.*$ http://domain/contacto.html [R=301,NC,L]
RewriteRule ^es(/.*)?$ / [R=301,NC,L]
The problem is that when I try to access to some pages like domain.com/empresa/testimonios-de-clientesc69a.html?lang=es it redirects me to domain/?lang=es , that is my domain+the parameter after ".html".
So, I was wondering if there was a way to eliminate this parameter and redirect only to www.domain.com ?
Thanks in advance!
To truncate a query string add a '?' sign to the end of target URLs like this:
RewriteRule ^es/empresa/.*$ http://domain/nosotros.html? [R=301,NC,L]
On Apache 2.4 or later you can use the QSD flag to discard the query string (see documentation):
When the requested URI contains a query string, and the target URI
does not, the default behavior of RewriteRule is to copy that query
string to the target URI. Using the [QSD] flag causes the query string
to be discarded.
So it would look like
RewriteRule ^es/empresa/.*$ http://domain/nosotros.html [R=301,NC,L,QSD]
Related
I am looking to include a rewrite rule for the following but can't seem to get it to work. I don't want to pass any query string in but I need to add one to the rule.
I want this URL:
https://example.co.uk/vehicles/
to point to:
https://example.co.uk/search-results/?category=1
but keep the first URL in the address bar.
I need to pass in a variable called category with a value.
I tried the following but it didn't work for me:
rewriterule ^vehicles/$ search-results/?category=1 [NC, L]
Any help would be appreciated.
RewriteEngine On
RewriteRule .* ? [F,L]
RewriteRule ^(.*)$ https://example.co.uk/$1 [R,L]
RewriteRule ^ad/(.*/)?([0-9]+)$ view-ad/?ad=$2 [NC,L]
RewriteRule ^vehicles/$ search-results/?category=1 [NC,L]
I managed to solve it. It was due to an Ajax load on the page.
Glad you solved your initial query, however, the following two directives in your posted .htaccess file will break your site, so presumably, these have already been removed?
RewriteRule .* ? [F,L]
RewriteRule ^(.*)$ https://example.co.uk/$1 [R,L]
The first directive simply blocks all access to your site, returning a 403 Forbidden response. And the second directive will result in a redirect loop.
Now that it works is it possible to have another rewrite rule that does this https://example.co.uk/vehicles/?something=1 and rewrite to https://example.co.uk/vehicles/?category=1&appendsomething=1 but only display https://example.co.uk/vehicles/
I assume you mean https://example.co.uk/search-results/?category=1&something=1 (as opposed to /vehicles/) - where something=1 is appended on the end of the query string?
You wouldn't be able to make this "display as https://example.co.uk/vehicles/" - as this would conflict with your existing (working) directive.
However, you could potentially modify your existing directive to handle requests for /?something=1 and pass this through to the substitution. This would simply require the addition of the QSA flag (Query String Append). For example:
RewriteRule ^vehicles/$ search-results/?category=1 [QSA,NC,L]
The QSA flag results in the query string from the request being appended to the end of the query string specified in the RewriteRule substitution.
UPDATE: To redirect HTTP to HTTPS, you would need something like the following instead:
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://example.co.uk/$1 [R,L]
Note the preceding RewriteCond directive - this ensures that only HTTP requests are redirected, not everything (HTTP and HTTPS), so avoiding a redirect loop. Ultimately this should also be a 301 (permanent) redirect, so you should change R to R=301, but only when you are sure it's working OK.
I am setting up Query string redirect :
expo.com/en/general/campaigns/on-second-thought.html?slide=ost-2016-tank to
expo.com/en/general/campaigns/on-second-thought/ost-2016-tank.html
RewriteCond %{HTTP_HOST} ^(.*)expo\.com
RewriteCond %{QUERY_STRING} slide=ost-2016-tank
RewriteRule ^/en/general/campaigns/on-second-thought.html?$ http://www.expo.com/en/general/campaigns/on-second-thought/ost-2016-tank.html [R=301,L,NC]
redirect happening but its appending ?slide=ost-2016-tank like below
http://www.expo.com/en/general/campaigns/on-second-thought/ost-2016-tank.html?slide=ost-2016-tank
slide=ost-2016-tank parameter is added to redirected page
Since your rule does not define a new query string, the default behavior of Apache is to copy the old query string to the new URL. To get rid of it, append a ? to the address you rewrite/redirect to:
RewriteRule ^/en/general/campaigns/on-second-thought\.html?$ http://www.expo.com/en/general/campaigns/on-second-thought/ost-2016-tank.html? [R=301,L,NC]
Or, for Apache >= 2.4, you can also use the flag QSD (Query String Discard):
RewriteRule ^/en/general/campaigns/on-second-thought\.html?$ http://www.expo.com/en/general/campaigns/on-second-thought/ost-2016-tank.html [R=301,L,NC,QSD]
Simply add a blank query string when redirecting:
RewriteCond %{HTTP_HOST} ^(.*)expo\.com
RewriteCond %{QUERY_STRING} ^slide=(ost-2016-tank)$
RewriteRule ^(/?en/general/campaigns/on-second-thought)\.(html)$ $1/%1.$2? [R=301,L,NC]
No need to mention http://expo.com again when redirecting. It'll automatically redirect to the same hostname because of R flag. No need to repeat same strings over and over. Using match groups and referencing them later works.
Your pattern had .html?$ in it, which actually means that it'll match .html as well as .htm. You do not receive query strings in RewriteRule context.
I've been struggling with some htaccess redirects. I just spent some time reading and searching and couldn't get a solution that works with my scenario.
I'm in the process of making the 301 redirect for an old website (ASP) to a new one (Wordpress). The old pages has parameters query which I need to process but also remove 'http://' string from it to get redirect to work.
Example URL (old) to redirect looks like:
http://www.domain.org/index.asp?documentID=2410&utm_source=IT+Travel+Reminder&utm_medium=Informz%2FnetFORUM&utm_campaign=IT%2FTravel+Reminder%2FMonthly+Monthly+Travel+Reminder&zbrandid=4050&zidType=CH&zid=28841368&zsubscriberId=1036792259&zbdom=http://my.informz.net
redirected to:
http://www.domain.org/permalink-2410/?qs=true&utm_source=IT+Travel+Reminder&utm_medium=Informz%2FnetFORUM&utm_campaign=IT%2FTravel+Reminder%2FMonthly+Monthly+Travel+Reminder&zbrandid=4050&zidType=CH&zid=28841368&zsubscriberId=1036792259&zbdom=my.informz.net
and .htaccess code to redirect it:
RewriteCond %{QUERY_STRING} ^documentid=2410(&.*)$ [NC]
RewriteRule ^index\.asp(.*):(.*)$ http://www.domain.org/permalink/?qs=true%1%2 [L,R=301,QSA]
but somehow is not working as I have expected when
RewriteCond %{QUERY_STRING} ^documentid=2410(&.*)$ [NC]
RewriteRule ^index\.asp$ http://www.domain.org/permalink/?qs=true%1 [L,R=301,QSA]
works fine when I will remove http:// or : from a query string.
Where do I have made mistake?
Thanks!
Try this rule:
RewriteCond %{QUERY_STRING} ^documentid=(\d+)(&.+?)http://(.+)$ [NC]
RewriteRule ^index\.asp$ http://www.domain.org/permalink-%1/?qs=true%2%3 [L,R=302,NC,NE]
Make sure to clear browser cache before testing this.
Let's say I have thiswww.example.com site structure:
/srv/http/
/srv/http/site/index.php
/srv/http/site/stuff.php
I want the following rewrites/redirects to happen:
www.example.com/index.php -> redirects to -> www.example.com/site/index.php -> but the user sees -> www.example.com/index.php
www.example.com/stuff.php -> redirects to -> www.example.com/site/stuff.php -> but the user sees -> www.example.com/stuff.php
In general, everything after www.example.com/ redirects to www.example.com/site/. But the user sees the original URL in the browser.
I've looked around on the internet but haven't managed to figure out what to use in this particular situation.
I tried rewriting everything:
RewriteEngine On
RewriteRule ^$ /site [L]
but index.php disappears and www.example.com/site/ is shown to the user.
How can I use .htaccess to solve this problem?
You need to capture the url request incoming into the server, like this:
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/site/
RewriteRule ^(.*)$ /site/$1 [L,QSA]
The QSA is (eventually) to also append the query string to the rewritten url
Same idea as #guido suggested, but a bit shortened using negative lookahead
RewriteEngine On
RewriteRule ^(?!site/)(.*)$ site/$1 [L]
Note: I am not using QSA flag as we are not adding additional parameters to the query string for the replacement URL. By default, Apache will pass the original query string along with the replacement URL.
http://www.example.com/index.php?one=1&two=2
will be internally rewritten as
http://www.example.com/site/index.php?one=1&two=2
If you really want add a special parameter (ex: mode=rewrite) in the query string for every rewrite, then you can use the QSA Query String Append flag
RewriteEngine On
RewriteRule ^(?!site/)(.*)$ site/$1?mode=rewrite [L,QSA]
Then this will combine mode=rewrite with original query string
http://www.example.com/index.php?one=1&two=2
to
http://www.example.com/site/index.php?mode=rewrite&one=1&two=2
RewriteEngine On
RewriteRule ^(.*)$ site/index.php?var=$1 [L]
With this rule, i'm passing all requests to site/index.php, so you could get the requested uri via $_GET['var'], and then you'll make the index.php serve the requested url behind the scene without the url changing in the user's browser. Ciao.
I was using a .htaccess redirect 301 to redirect a URL which then appended all the query string elements to the end, leaving me with some URLs indexed in Google as /store/product/?d=department&s=section&p=product.
I have fixed the redirect by using a RewriteRule instead which doesn't append the query strings, however I'm stuck trying to rewrite the old redirected URLs with the query strings back to the original URLs (as these are looking like two different URLs to Google now).
I have managed to get a RewriteRule to sort of work, in that /store/product/xxxxx redirects to /store/product/ as it should, it just doesn't seem to work with the whole query string of.
What I have been using is:
RewriteRule ^store/product/([a-zA-Z0-9\-_\?=&]+)$ http://www.example.com/store/product/ [NC,R=301,L]
or
RewriteRule ^store/product/\?d=department&s=section&p=product$ http://www.example.com/store/product/ [NC,R=301,L]
Hope that all makes sense!
Many thanks
RewriteCond %{QUERY_STRING} !^$
RewriteRule ^store/product/.*$ http://www.example.com/store/product/? [NC,R=301,L]
You need to specify an empty query in your substitution to not have the original one automatically appended to the new URL:
RewriteRule ^store/product/[a-zA-Z0-9\-_=&]+$ http://www.example.com/store/product/? [NC,R=301,L]
Note the ? at the end of the substitution URL.