How to add content HTTP_HOST variable to query string - php

i need to create a rewrite rule/condition that always rewrites the http_host to the query string.
i have multiple domains and all of them point to the same DocumentRoot.
What i want to do now is, to add the called domain-name (e.g.) example1.org to the query-string for application internal use. Let's say the application is situated at baseapplication.org
opening
example.org
in my browser runs into document root and htaccess should rewrite it internally to:
baseapplication.org?requested_domain=example1.org
i cannot find a combination of rewrite rules or conditions to get that running.

Im not a .HTACCESS pro, but this should do the trick.
<IfModule mod_rewrite.c>
Options +FollowSymLinks
RewriteEngine On
</IfModule>
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>
have that in your root, in a file named .HTACCESS along with a index.php file.
Then in the index file you can access all the request vars.
From here you can either do a php headers redirect, or include the file from another dir.
up to you.
Not a solution, but should help a little

From the Apache documentation:
Description:
Assume that you want to provide www.username.host.domain.com for the homepage of username via just DNS A records to the same machine and without any virtualhosts on this machine.
Solution:
For HTTP/1.0 requests there is no solution, but for HTTP/1.1 requests which contain a Host: HTTP header we can use the following ruleset to rewrite http://www.username.host.com/anypath internally to /home/username/anypath:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.[^.]+\.host\.com$
RewriteRule ^(.+) %{HTTP_HOST}$1 [C]
RewriteRule ^www\.([^.]+)\.host\.com(.*) /home/$1$2
There are a handful of other HTTP rewrites at the Apache Rewrite Guide.

Related

Redirect to a single file in apache

I have an index.php file in the DocumentRoot of a virtualHost in apache. It contains :
$path = $_SERVER['REQUEST_URI']
if($path == '/')
echo json_encode(some_data);
else if ($path == '/posts')
echo json_encode(another_data);
the thing is when I send a get request to / it respond with the data I wanted, but when requesting /posts or any endpoint other than /, the server respond with not found page.
How can I make it use index.php for every request that virtualhost ?
I want all the requests to be handled by index.php
WordPress deals with that by adding this to the .htaccess file:
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
Basically, you need to redirect all requests to index.php, but not using a 301 or 302, just rewriting. The RewriteCond lines are to make it so users can still access other files directly if they exist. You may not need those in your implementation.
Here's an explanation of the code:
RewriteEngine On turns on the rewrite engine :)
RewriteBase / Just makes sure we're starting from the document root
RewriteRule ^index\.php$ - [L] is a rule that does nothing if the request is for index.php. the [L] means no more rules will be processed.
The two RewriteCond lines make sure that there's not a matching file (1st line) or directory (2nd line) for the request. If there is, then the following RewriteRule won't take effect, and the server will serve up that file or directory. Otherwise:
RewriteRule ^index\.php$ - [L] will rewrite the request to /index.php, making your script handle the request without redirecting the user's browser to index.php.
If there is no .htaccess file, you'll need to add one, or add the rules to the conf file for this virtualhost. The conf file will also need to allow overrides to see the .htaccess file:
<Directory "/path/to/document/root">
AllowOverride All
</Directory>
You can use FallbackResource:
FallbackResource /index.php
Set it in your virtual host settings or in an .htaccess file.

Sending specific folder to HTTPS and the rest from non-www to www, while keeping the rest of the laravel routes working?

I am trying to write a .htacess file such that:
The site runs php 5.4
Requests to domain.com run index.php first
Requests to http://domain.com/checkout are redirected to https://domain.com/checkout
All requests to domain.com are redirected to www.domain.com
So here is my attempt:
<IfModule mod_rewrite.c>
# Use PHP 5.4
AddType application/x-httpd-php54 .php
Options -MultiViews
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
RewriteCond %{HTTP_HOST} !^www\.domain\.com
RewriteRule ^(.*)$ http://www.domain.com/$1 [R=permanent,L]
RewriteCond %{HTTPS} !on
RewriteCond %{REQUEST_URI} ^(/checkout|/order)
RewriteRule ^(.*)$ https://www.domain.com/$1 [R,L]
</IfModule>
But when I make a post to domain.com/cart the user is automatically redirected to domain.com/index.php
Please let me know where I am going wrong...
First off, you need to understand that such rules in .htaccess files are run on a first-come, first-serve basis. Also, you are using the L flag for each of them.
So, you should make sure that the two conditions and rule for silent-mapping to index.php should come last. Move those down to below the https and www-on rules.
Now, of course a request to /cart will be mapped to index.php; that's what it's programmed to do. However, you say "redirected"... Does that mean that it shows index.php in the address bar? If that is the case, the fix I've mentioned should sort that out (always check for redirects first, and then do the necessary mapping).
Like #scotsninja said in the comments, these things should be handled by Laravel itself. Your .htaccess file should only be used to map anything that is not a file or directory to the index file, or Laravel bootstrap.

.htaccess PHP Controller Internal Server Error

I'm creating a PHP controller for testing purposes on my domain. The domain, by default, is entirely on Wordpress and I'm having issues setting up just the folder testing1 to be controlled by my PHP controller. Here's the code in my .htaccess file:
# BEGIN WordPress
<IfModule mod_rewrite.c>
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php
RewriteRule ^testing1/(.*)$ ./testing1/controller.php
</IfModule>
# END WordPress
When I go to mydomain.com/testing1/ I get an internal server error. Any help is appreciated. Thanks!
Get rid of ./, which is used to point to current directory.
RewriteRule ^testing1/(.*)$ testing1/controller.php
I have it working.
The RewriteRules are executed in order. If one does match, the rest is ignored.
So the result might look like this (just the RewriteRules):
RewriteRule ^testing1/(.*)$ /testing1/controller.php
RewriteRule . /index.php
The second rule does mean that when you type exactly one character (whatever it is), you will get directed to index.php. I don't know though if that is your intended behaviour.

.htaccess in subdirectory 'overriding' parent htaccess

been searching for 2 days and can't quite get the right solution due to my lack of understanding of mod_rewrite and time constraints on this project so hoping someone can help.
The aim
To rewrite all requests to the root index.php if the client doesn't have the correct cookie.
If the client has the correct cookie allow them to browse as they wish.
The problem
The htaccess in my subdirectory is taking precendence over my root htaccess, so requests such as www.mydomain.com/subdir/index.php arn't getting redirected.
My root .htaccess
Options FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_COOKIE} !^.*pass.*$
RewriteCond %{REQUEST_URI} !^/index.php$
RewriteRule ^(.*)$ http://www.mydomain.com/index.php?url=$0 [NC]
My subdir htaccess
RewriteEngine On
RewriteBase /
RewriteCond $1 !\.(gif|jpe?g|png)$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?/$1 [L]
Additional info
Ideally I'm trying to create a password protected area, so all requests are routed to index.php where a password can be entered and when verified a cookie is created, allowing free browsing of contents and sub directories. So if there is a better way to accomplish this then please let me know, and I havn't gone for .htpasswd since I need custom login, error and splash pages.
Also, the subdir .htaccess is an ExpressionEngine URL handler.
Thanks.
To allow execution of rewrite rules from parent .htaccess (htaccess from parent folder), you need to explicitly allow it (Apache will treat rewrite rules in current .htaccess as the only one that need to be executed, as long as rewritten URL remains in the same subfolder).
You need to add this line to your .htaccess in sub-folder:
RewriteOptions inherit
Apache manual: http://httpd.apache.org/docs/current/mod/mod_rewrite.html#rewriteoptions

How does Wordpress rewrite the URL without a rewrite map?

The .htaccess file for a wordpress site looks something like this:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
...and there is no rewrite map set in htdocs. How does this work? How does Apache know how to rewrite these url?
The Apache does not know. All the requests are sent to index.php and Wordpress keeps an internal log of which page to redirect where, and it redirects it. So, in essence, Wordpress actually has two sets of rewrite rules, one internally and a "greedy" external rule in your .htaccess which basically makes all requests refer to the internal rewrite rules.
You may be interested in using this plugin which shows all the internal rewrites that Wordpress is doing itself.

Categories