Drupal .htaccess rewrite/substitution without redirect - .htaccess

I have a URL as follows: www.site.com/catalog/?type=6 which I need to rewrite to www.site.com/white-wines/white-cases.
What is important is that it is not a redirect, for example the URL should remain as www.site.com/white-wines/white-cases when visited, but show content from www.site.com/catalog/?type=6.
The current solution that I have performs a redirect and I'm struggling to work this out.
RewriteRule ^white-wines/white-cases$ http://www.site.co.uk/catalog/?type=6 [L]

Setting full address in the target implies a redirect if the target is different then the current hostname, not rewrite. Try root-relative path.
RewriteRule ^white-wines/white-cases$ /catalog/?type=6 [L]
You can't do a re-write to a different hostname.
Absolute URL
If an absolute URL is specified, mod_rewrite checks to
see whether the hostname matches the current host. If it does, the
scheme and hostname are stripped out and the resulting path is treated
as a URL-path. Otherwise, an external redirect is performed for the
given URL. To force an external redirect back to the current host, see
the [R] flag below.
Source: Apache Module mod_rewrite

Related

Need to redirect last part of url using htaccess with parameters

#RewriteRule #htaccess
I have a url for eg: www.example.com/path1/path2/pdfname.pdf. i need to redirect this url to another with the pdf name without pdf extension like(pdfname). Redirect Url should be www.example.com/path3/viewpdf.php?param=pdfname.
Would appreciate your help, Thanks.
Assuming that "path1", "path2" and "path3" are all fixed, literal strings that probably is what you are looking for:
RewriteEngine on
RewriteRule ^/?path1/path2/(.+)\.pdf$ /path3/viewpdf.php?param=$1 [L]
This keeps the URL visible in the browser unchanged, which usually is what is desired.
That rule will work likewise in the central http server's host configuration (which usually is preferred) or, if you do not have access to that, in a distributed configuration file (often called ".htaccess"). In the later case that file needs to be readable by the http server process and it has to be located in the DOCUMENT_ROOT folder of the processing http server's host.
If instead of an internal rewrite you really want to redirect the request (so change the URL actually visible in the browser), then that variant should do:
RewriteEngine on
RewriteRule ^/?path1/path2/(.+)\.pdf$ /path3/viewpdf.php?param=$1 [R=301,L]
Again no domain name (http host name) or protocol scheme has to be specified if they stay the same. The same hints as above apply.

How can I mask a domain using .htaccess?

We have the following situation:
We would like to setup a domain masking to provide content from a project platform to an end user. The end user has setup a CNAME record from player.domain-client.com. to app.domainA.com
Now when the end user enters https://player.domain-client.com/5432 he should get the contents of https://app.domainA.com/player/?=5432.
But the URL should remain https://player.domain-client.com/5432.
This masking should only by applied if the client subdomain contains player.
Could anybody point me to the right direction on how to setup the .htaccess so it does the correct masking?
The end user has setup a CNAME record from player.domain-client.com. to app.domainA.com
Presumably the "project platform" has also been configured to accept requests to player.domain-client.com?
In which case, it should just be a matter of a simple internal rewrite (on the same host). Although, if you would ordinarily request the same URL-path at app.domainA.com , ie. app.domainA.com/5432, then there is nothing you need to do as the rewrite is already in place? Otherwise, try the following:
RewriteEngine On
# Rewrite any request for /<number> to player/?=<number>
RewriteCond %{HTTP_HOST} ^player\. [NC]
RewriteRule ^(\d+)$ player/?=$1 [L]
However, /player/?=5432 isn't the actual endpoint as this requires further rewriting by the system for it to "work". Perhaps you mean something like /player/index.php?=5432? (The query string is also a little weird as you are missing a parameter name? As written, this would possibly require manual parsing of the query string to extract the value?)
The condition (RewriteCond directive) ensures that only requests to the player subdomain are rewritten.
On WordPress you need to make sure these directives go before the WP front-controller. ie. Before the # BEGIN WordPress section. The order of directives in .htaccess is important.
However, if this is all being managed by WordPress then you can't simply create a rewrite in .htaccess since WordPress still sees the original URL that was requested, not the rewritten URL. So, unless the requested URL exists as a valid route in WordPress itself then you'll likely get a 404. This sort of rewrite needs to be managed inside WordPress itself.
Alternative solution using a reverse proxy
An alternative is to configure your server as a reverse proxy and proxy the request from https://player.domain-client.com/1234 to https://app.domainA.com/player/?vid=1234 (mentioned in comments). Ideally this requires access to the main server config to config properly (requires mod_proxy and ProxyPass, ProxyPassReverse directives set appropriate in the virtual host).
Then, in .htaccess you would do something like the following instead, making use of the P flag on the RewriteRule:
# Proxy any request for /<number> to player/?=<number>
# for the "player" subdomain only.
RewriteCond %{HTTP_HOST} ^player\. [NC]
RewriteRule ^(\d+)$ https://app.domainA.com/player/?vid=$1 [P]

htaccess Rewrite rule to send variable info to script but display clean url

What I'd like to do is when a URL like
http://localhost/sandbox/jcsearch/country/countryname
is typed by the user I would like to to point to
http://localhost/sandbox/jcsearch/index.php?country=countryname
but still retain the original clean URL in the address bar ie
http://localhost/sandbox/jcsearch/country/countryname
Is this possible? would it create any kind of redirect loop?
The rewrite would happen as follows:
RewriteEngine on
RewriteRule ^sandbox/jcsearch/([^/]+)/([^/]+)$ sandbox/jcsearch/index.php?$1=$2 [L,NC]
Since, the RewriteRule directive does not have the redirection flag (R) set, it will not change the URL in your browser's addressbar. So, by visiting
http://localhost/sandbox/jcsearch/country/countryname
user will get internally redirected to:
http://localhost/sandbox/jcsearch/index.php?country=countryname
Please note: You have to put the rewrite rules in the htaccess file in your server root directory.

Rewrite url in browser

Hi how do I rewrite URL in .htaccess?
localhost/app/view/login/editPass.php
to
localhost/editPass.php?
when I come from my index
localhost/index.php
I know I have to use, something like
RewriteEngine On
RewriteRule ^(.*)
So do I need to have the .htaccess file with my index or in the folder app?
The .htaccess should be on the parent directory. RewriteRule ^/editPass.php?$ app/view/login/editPass.php
It must have the RewriteEngine On On top first. The "RewriteRule" line is where the magic happens. The line can be broken down into 5 parts:RewriteRule - Tells Apache that this like refers to a single RewriteRule.
^/editPass.php?$
The "pattern". The server will check the URL of every request to the site to see if this pattern matches. If it does, then Apache will swap the URL of the request for the "substitution" section that follows.
app/view/login/editPass.php
The "substitution". If the pattern above matches the request, Apache uses this URL instead of the requested URL.

Simple and neat .htaccess redirect help required

This is a strange one...
A while back I managed to write a .htaccess redirect that worked so that the URL was read like: www.website.com/mt?page=index - and what the real URL of this page was www.website.com/PageParser.php?file=index.php
The problem has been that the FTP system of my webhost hides .htaccess files even though they are allowed and do operate - and so I have checked back on local copies I have of my .htaccess files and none of them have the code as to how this works - and I've forgotten how I did it!!
Essentially, I am using wildcards so that anything after mt?page= will actually be showing PageParser.php?file= but without having the PageParser.php showing within the URL (and this is the important bit, because the index.php on my site root is actually sent through PageParser.php first so that anything which shouldn't be there is wiped out before the end user sees it) - so how can .htaccess redirect/rewrite the URL so that any link to /mt?page= show the file located at /PageParser.php?file= without changing the URL the user sees?
RewriteEngine On
RewriteRule ^(.*)mt?page=(.*)$ $1PageParser.php?file=$2
RewriteEngine On
RewriteBase /
RewriteCond %{QUERY_STRING} ^page=([^&]+)
RewriteRule ^mt$ /PageParser.php?file=%1.php [NC,L]
This rule will rewrite (internal redirect) request for /mt?page=hello to /PageParser.php?file=hello.php without changing URL in browser.
Your source URL example (www.website.com/mt?page=index) has index while target URL (www.website.com/PageParser.php?file=index.php) has index.php. The above rule will add .php to the page name value, so if you request /mt?page=hello.php it will be rewritten to /PageParser.php?file=hello.php.php.
If there is a typo in your URL example and page value should be passed as is, then remove .php bit from rewrite rule.
The rule will work fine even if some other parameters are present (e.g. /mt?page=hello&name=Pinky) but those extra parameters will not be passed to rewritten URL. If needed -- add QSA flag to rewrite rule.
This rule is to be placed in .htaccess in website root folder. If placed elsewhere some small tweaking may be required.
P.S.
Better write no explanation (I knew it/I did it before .. but now I forgot how I did it) than having these "excuses". While it may be 100% true, it just does not sound that great.

Resources