.htaccess - silently rewrite/redirect everything to internal subfolder - apache

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.

Related

Can't pass parameters to another redirect website

I want to redirect to another website, and pass the parameters also.
Example: I go to my website: source.example/?code=12345
Then, I want it to redirect to target.example/?code=12345.
I am currently using this for my .htaccess file, since I figured out from other posts that if I query a certain parameter, it will get passed also:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^code=[NS]$
RewriteRule "www.google.com" /$1 [R=302,L]
Also, I tried many different approaches looking at these stack questions:
simple .htaccess redirect : how to redirect with parameters?
Redirect and keep the parameter in the url on .htaccess
But I can't get it running :(
since I figured out from other posts that if I query a certain parameter, it will get passed also
This is not true. The query string is passed through by default - there is nothing extra you need to do if you want the same query string on the target URL.
RewriteCond %{QUERY_STRING} ^code=[NS]$
RewriteRule "www.google.com" /$1 [R=302,L]
This code won't match the source URL for many reasons:
"www.google.com" - The first argument to the RewriteRule directive is a regex that matches the source URL-path (less the slash prefix). In your example the URL-path is empty.
^code=[NS]$ matches either code=N or code=S - which is not the intention from your example. (The [NS] looks like a mangled RewriteRule flag?!)
/$1 - this is the substitition string, ie. the URL you want to redirect to. (The $1 backreference is always empty, so this is meaningless.)
To redirect from source.example/?code=<number> to https://target.example/?code=<number> then try the following instead:
RewriteCond %{HTTP_HOST} ^source\.example [NC]
RewriteCond %{QUERY_STRING} ^code=\d+$
RewriteRule ^$ https://target.example/ [R=302,L]
This only matches a query string of the form code=1234. It does not match code= or code=1234&foo=bar, etc.
The query string is passed through by default.
If source.example is the only domain being hosted at the current location then you can remove the first condition that explicitly checks the requested hostname.
The order of directives in the .htaccess file is important. An external redirect like this should go near the top.

htaccess rewrite rule from folder to folder with querystring

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.

Redirect omitting parameters in .htaccess

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]

Redirect www to non-www loses Google gclid tracking code

I've used the .htaccess code in Generic htaccess redirect www to non-www to successfully redirect www.mysite.com to mysite.com, however in the process it strips off the gclid parameter and now AdWords isn't counting clicks correctly.
Is it possible to amend the code below so that any parameters are retained after the redirect?
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
From: https://wiki.apache.org/httpd/RewriteFlags/QSA
QSA = Query String Append.
This rule appends the GET query string which results from the ReWrite rule to the initial GET query string sent by the browser. For example, take the following RewriteRule:
RewriteRule ^/product/([0-9]*)/? /product.php?product_id=$1 [QSA]
This simply makes the product_id number look like a directory to the user. Now say that I have two different views of the page, view=short and view=long. For whatever reason, I don't want to make these views look like directories by using a RewriteRule. So I want to be able to do things like:
http://example.com/product/1351283/?view=short
Let's see how QSA works. With QSA, my final rewritten URL is
http://example.com/product.php?product_id=1351283&view=short
QSA has caused the RewriteEngine to append the existing query string (view=short) to the new query string (product_id=1351283). Without QSA, the existing query string is simply replaced by the new query string:
http://example.com/product.php?product_id=1351283
If you do much scripting with reliance on GET variables, it is virtually imperative that you enable the QSA flag on all of your RewriteRules.

.htaccess mod_rewrite linking to wrong page

I have in my .htaccess the following code:
RewriteEngine On
RewriteRule ^/?([^/\.]+)/?$ $1.php [L]
RewriteRule ^/?([^/\.]+).php$ $1/ [R,L]
RewriteRule ^/?([^/\.]+)/?$ $1.php [L] is working fine. What this is doing is taking a url like http://www.example.com/whatever and making it read the page as http://www.example.com/whatever.php.
However, what I'd like to be able to do is take a url like http://www.example.com/whatever.php and automatically send it to http://www.example.com/whatever, hence the second line of the code. However, this isn't working. What its doing now, is as soon as it comes across a link ending in .php, the url becomes http://localhost/C:/Sites/page/whatever/, and pulling a 403: Forbidden page.
All I want to know is what I can to so that http://www.example.com/whatever.php will be read as http://www.example.com/whatever, and that if http://www.example.com/whatever.php is entered into the URL bar, it will automatically redirect to http://www.example.com/whatever.
Does that make any sense?
EDIT
Ok, so it appears I wasn't all too clear.. basically, I want /whatever/ to read as whatever.php while the URL still stays as /whatever/, right? However, if the URL was /whatever.php, I want it to actually redirect the users URL to /whatever/, and then once again read it as whatever.php. Is this possible?
If you're rules are inside an .htaccess file, you can omit the leading slash when you match against a URI:
RewriteRule ^([^/\.]+)/?$ /$1.php [L]
Also note that a leading slash is included in the target (/$1.php), this makes sure /whatever/ gets rewritten to /whatever.php. When you redirect, if you are missing this leading slash, apache prepends the document root to it. Thus /whatever.php gets redirected to the document root C:/Sites/page/whatever/. Even if you include the leading slash, this will never work because you're going to cause a redirect loop:
Enter "http://www.example.com/whatever.php" in your address bar
apache redirects you to "http://www.example.com/whatever/"
apache gets the URI whatever/ and applies the first rule and the URI gets rewritten to /whatever.php
The URI gets put through the rewrite engine again
the URI /whatever.php matches the second rule and redirects the browser to "http://www.example.com/whatever/"
repeat steps 3-5
You need to add a condition that the actual request is for /whatever.php:
RewriteCond %{THE_REQUEST} ^(GET|POST|HEAD)\ /([^/\.]+)\.php
RewriteRule ^ /%2/ [R,L]
So altogether, you'll have:
RewriteEngine On
RewriteRule ^([^/\.]+)/?$ /$1.php [L]
RewriteCond %{THE_REQUEST} ^(GET|POST|HEAD)\ /([^/\.]+)\.php
RewriteRule ^ /%2/ [R,L]
You're making a relative path substitution in a per-directory context (.htaccess is a per-directory context). This requires RewriteBase. Per-directory rewrites are done in a later stage of processing, when URLs have been mapped to paths. But the rewrite must produce a URL, which is processed again. I think without the RewriteBase to supply the URL prefix, you end up with a filesystem prefix instead of the URL. That may be why you're getting the C:/Sites thing. Try RewriteBase. But after a correct RewriteBase to specify the correct URL prefix to be tacked in front to the relative rewritten part, I'm afraid you will have the rewrite loop, because you're rewriting whatever.php to whatever; and whatever to whatever.php.
Reference: http://httpd.apache.org/docs/current/rewrite/tech.html