Redirecting a subdomain with .htaccess - php

I have a subdomain set up on my hosting: indiantimes.indianradio.net.au, that is being pulled from a folder in my /public_html folder: /public_html/indiantimes.com.au.
I am trying to write an .htaccess rule that will redirect it to that folder still, but retain the original url the user typed in: indiantimes.indianradio.net.au.
I have only been able to get the redirect working, i.e. (indiantimes.indianradio.net.au redirects to indianradio.net.au/indiantimes.com.au/), but I can't seem to get the redirect working so the url seen by the user, stays at: indiantimes.indianradio.net.au. The majority of the image urls are broken intil I am able to get the redirect working properly.
The .htaccess rule I was playing around with was:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^indiantimes\.indianradio\.net\.au$ [OR]
RewriteCond %{HTTP_HOST} ^www\.indiantimes\.indianradio\.net\.au$
RewriteRule ^/?$ "http\:\/\/indianradio\.net\.au\/public_html\/indiantimes\.com\.au" [R=301,L]
What am I doing wrong with the redirect? Any help would be much appreciated! Thanks in advance!

You have to replace your sub folder name to be the same as your sub domain
(indiantimes.com.au -> indiantimes).
RewriteEngine On
RewriteCond %{HTTP_HOST} ^indiantimes\.indianradio\.net\.au$
RewriteCond %{REQUEST_URI} !^/indiantimes/
RewriteRule (.*) /indiantimes/$1
source

for subdomains it is generally recommended to add a virtualhost in apache instead of using .htaccess (preformance-wise and more cross-platform).
However you might find the following link suitable in case editing the apache config files isn't an option: .htaccess rewrite subdomain to directory (summary: using mod-proxy and adding the P flag to your RewriteRule)
Go to /etc/apache2/sites-available (use cd in terminal)
Add a new file named: indiantimes.indianradio.net.au, example content:
<VirtualHost *>
DocumentRoot /var/www/indianradio.net.au/public_html/indiantimes.com.au/
ServerName indiantimes.indianradio.net.au
<Directory /var/www/>
Options Indexes FollowSymLinks MultiViews +Includes
AllowOverride None
Order allow,deny
allow from all
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error-logfile.log
# Possible values include: debug, info, notice, warn, error, crit,
# alert, emerg.
LogLevel warn
CustomLog ${APACHE_LOG_DIR}/access-logfile.log combined
</VirtualHost>
link to the file in apache2/sites-enabled
in terminal: ln -s ./indiantimes.indianradio.net.au ../sites-enabled/ from the sites-available folder, note the trailing /!

Related

Apache2 .htaccess rewrite rule for nested URLs doesn't work

I have simple php application with navigation based on domain/foo/bar nested urls.
For instance, I have main page index.php with about nav link which should navigate to domain/en/about, where en and about must be transfered to url param like index.php?url=....
But when I click to about I got to domain/en/aboutand
404 not found instead.
I have configured apache2 virtual domain config as:
<VirtualHost *:80>
ServerAdmin webmaster#localhost
<Directory /var/www/html/domain>
Options -Indexes +FollowSymLinks -MultiViews
AllowOverride All
Require all granted
</Directory>
DocumentRoot /var/www/domain/
ServerName domain.local
ServerAlias www.domain.local
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
And .htaccess file as:
order deny,allow
RewriteEngine On
RewriteBase /
RewriteRule .* index.php?url=$0 [QSA,L]
mod_rewrite for apache2 is already enabled.
Have no clue what I have missed.
Any help is appreciated!
Thank you in advance!
<Directory /var/www/html/domain>
:
DocumentRoot /var/www/domain/
Your <Directory> section and DocumentRoot directive refer to different locations, so regardless of where you've put the .htaccess file, it's not going to work as intended.
However...
RewriteRule .* index.php?url=$0 [QSA,L]
This rule is not strictly correct, since it ends up rewriting itself on a second pass by the rewrite engine. If it wasn't for the QSA flag, the original url param value (that contains the originally requested URL-path) would be lost. The above ends up rewriting a request for /en/about to index.php?url=index.php&url=en/about. Fortunately, your PHP script still reads $_GET['url'] as en/about. But you can examine the full (erroneous) query string in $_SERVER['QUERY_STRING'].
(And, if you were to simply prefix the substitution string with a slash, ie. a URL-path, you'll get an endless rewrite-loop (500 Internal Server Error). But this could also result from adding additional rules later.)
You should prevent requests to index.php itself being rewritten, which you can do by adding an additional rule. For example:
RewriteRule ^index\.php$ - [L]
RewriteRule .* index.php?url=$0 [QSA,L]
However, this will still rewrite your static assets (assuming you are linking to internal images, CSS and JS files?). So, you would normally need to prevent this with an additional condition that prevents the rule from being processed if the request already maps to a static file.
For example:
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule .* index.php?url=$0 [QSA,L]
The CondPattern -f checks if the TestString maps to a file. The ! prefix negates this. So the condition is only successful when the request does not map to a file.
You need parenthesis around what you want to capture. Back-references indices start with '1':
RewriteRule (.*) index.php?url=$1 [L,QSA]

Apache and TYPO3 not loading pages other than home-page and backend

I am using Apache 2 on Xubuntu to run multiple local instances of TYPO3. Since they use different TYPO3 and therefore PHP versions, I'm using fastcgi to pass requests to the one with TYPO3 version 9.5.x to the corresponding php7.2-fpm.
However, none of the pages other than "Home", the TYPO3 backend and the phpinfo are loading. I just get a raw 404 message which looks like it's coming from apache rather than from TYPO3 itself.
The only way I can get the pages to load is when I call them using the pageID and a parameter to suppress the redirect to the "more beautiful" URL. This, and the raw error page, make me believe that the problem lies within my Apache config rather than my TYPO3 setup. It seems like every call to a specific file (/index.php, /typo3/index.php, and /info.php) works, but the routing doesn't work because the apache tries to resolve it directly to a file/directory.
This is my apache config for the problematic vhost:
<VirtualHost *:80>
ServerName test.test
DocumentRoot /var/www/foo/bar/httpdocs
# <FilesMatch \.php$>
# SetHandler "proxy:unix:/var/run/php/php7.2-fpm.sock|fcgi://localhost"
# </FilesMatch>
ProxyPassMatch ^/(.*\.php(/.*)?)$ unix:/run/php/php7.2-fpm.sock|fcgi://localhost/var/www/foo/bar/httpdocs
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
The commented part was the first try, when I noticed the problems I found the "ProxyPassMatch"-Part online and tried it out, but I have the same issues.
This is the crucial part of the rewriting in the default .htaccess (that is shipped with TYPO3):
<IfModule mod_rewrite.c>
RewriteEngine On
# ...
# If the file/symlink/directory does not exist => Redirect to index.php.
# For httpd.conf, you need to prefix each '%{REQUEST_FILENAME}' with '%{DOCUMENT_ROOT}'.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^.*$ %{ENV:CWD}index.php [QSA,L]
</IfModule>
Important is that all URLs that are not file requests are passed on to index.php
First, check the obvious:
Is mod_write enabled (e.g. a2enmod rewrite)?
Is the .htaccess executed? As mentioned, in the comments, AllowOverride must be set.
For example, within the VirtualHost section, put this:
<Directory /var/www/foo/bar/httpdocs>
# When this directive is set to All, then any directive which has the .htaccess Context is allowed in .htaccess files.
AllowOverride All
</Directory>
If that does not solve the problem, you can try narrowing down the problem step by step:
For example create a file /abc.txt with text "hello" in your web root. Then, add this before the TYPO3 rewrite blocks.
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule /?xyz /abc.txt [L]
</IfModule>
Test the URLs /abc.txt, /xyz. They should both show "hello".
This way you can check if the rewrites work in general (without involving TYPO3). Once the test is successfull, remove again.
see also Troubleshooting in the Installation Guide, which also mentions the mod_rewrite.
Disclaimer: I don't know about the ProxyPassMatch but it seems to be generally working from what you described.

PHP and Redirect with apache2.conf

I have an internal domain like
live.domain.com
which I defined in sites-available like so
ServerName live.domain.com
ServerAdmin webmaster#localhost
DocumentRoot /data/www/html/mydomainnamenodots
<Directory /data/www/html/mydomainnamenodots>
Require all granted
</Directory>
and the accordingly same for SSL conf.
I do have a folder /api which is in there like
/data/www/html/mydomainnamenodots/api
What I want is to catch all calls to
live.domain.com/api/*
and redirect them to something like
live.domain.com/api/index.php?url=*
So a real example would be
live.domain.com/api/statistics/getFTE/123/456
and I'd like that to be
live.domain.com/api/index.php?url=/statistics/getFTE/123/456
That would result in a $_REQUEST in index.php like so
array(1) {["url"]=>string(33) "/statistics/getFTE/123/456"}
mod_rewrite is enabled and loaded. Simple tests with file to file redirects work.
My rewrite rule which does not work as it does not redirect is
<Directory "/data/www/html/mydomainnamenodots/api">
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^.*?mydomainnamenodots\/api(.*)$ /mydomainnamenodots/api/index.php?url=$1 [QSA,L]
</Directory>
Any ideas why that does not work?
Am I missing the RewriteBase or something?
Thanks in advance
Alex
As the subdomain already points to a subfolder, it has to be the the according subfolder below that one in the directory definition. In addition I changed the regexp as well.
Here is what worked for me:
<Directory />
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^.*\/api(\/.*)$ /api/index.php?url=\/$1 [QSA,L]
</Directory>

php links not working in sub folder of multi site

Testing two identical folders , one placed in a Drupal install folder and the other in a plain html subfolder in
home root , only the php links for the one in CMS install work. This makes sense because Drupal has a settings php
file and .htaccess file which makes sure everything is in its proper place for links to work whether in home root or not. However I thought it would be easy enough to get the links to work in plain html subfolder with a simple rewrite
rule in the .htaccess file which exists in the php folder. Yet try as I might, nothing has worked so far.
The configuration in /etc/httpd/conf.d/my.conf
<VirtualHost *:80>
ServerName drupalsite.com
ServerAlias www.drupalsite.com
ServerAdmin vps#drupalsite.com
DocumentRoot "/var/www/html/drupalsite.com"
<Directory /var/www/html/drupalsite.com>
AllowOverride All
Allow from all
</Directory>
</VirtualHost>
<VirtualHost *:80>
ServerName plainhtml.com
ServerAlias www.plainhtml.com
ServerAdmin vps#plainhtml.com
VirtualDocumentRoot "/var/www/html/plain”
</VirtualHost>
The .htaccess file in identical php folders
RewriteEngine On
RewriteBase /phpfolder/
RewriteRule ^c-(.*)$ catpost.php?id=$1 [L]
RewriteRule ^a-(.*)-(.*)$ archives.php?month=$1&year=$2 [L]
RewriteCond %{REQUEST_FILENAME} !-d [NC]
RewriteCond %{REQUEST_FILENAME} !-f [NC]
RewriteRule ^(.*)$ viewpost.php?id=$1 [QSA,L]
So drupalsite.com/phpfolder links all work
But plainhtml.com/phpfolder links do not work
phpfolder for Drupal resides in /var/www/html and Drupal site is symlinked
phpfolder for plain html subfolder to root resides in /var/www/html/plain
Both phpfolders are owned by apache:root and all files within are owned by root:root
Have also tried changing the .htaccess rewrite rule to:
RewriteBase /
RewriteBase /plain/
RewriteBase /plain/phpfolder/
You will have to define a Directory entry with AllowOverride for:
/var/www/html/plain
<Directory /var/www/html/plain>
AllowOverrideAll
Allow from all
</Directory>
Note: If you have access to the main configuration file of the server you shouldn't be using .htaccess, configuring Rewrites or Redirects in Virtualhost is simpler. As you have already seen, using sub-files and per-dir configurations make things more complicated, not counting the overhead of apache having to constantly check those files when AllowOverride is enabled.

How to get access of other pages inside of index.php by PHP?

I installed PHP and Apache server in my computer.
So inside of "htdocs" I created 2 files(index.php, Contact.php) and a directory(MyClass), after that inside of "MyClass" I created a file(class.php)..
In web browser when I am using the url "http://localhost/MyClass/class.php", the result is : "class.php" sending data to the web browser.
In the same situation is there any way in PHP/Apache to take control of it from the "index.php" ??
Or
I want to be known about all the requests inside of "index.php" which came from web browser, is it possible ????
But I don't want to use any GET variable like "http://localhost/index.php?class=page2"..
Apology for my bad English.
Thanks..
You should use include, in your case you would use
include 'MyClass/class.php';
More information about include can be found right here
I'm not sure I understand correctly but a way to not use ?class=page2
is to create a .htaccess file
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
This will rewrite all requests to non existing files or folders to your index.php
the use $_SERVER['REQUEST_URI'] to make your navigation.
for example you could use http://localhost/class/page/2
$_SERVER['REQUEST_URI'] would then be class/page/2
If your website is in a subfolder of htdocs be sure to edit
RewriteBase /dir/here/
[...]
RewriteRule . /dir/here/index.php [L]
to match it
My problem solved.
Which changes I did they are below :
In "C:\Apache24\conf" need to change file "httpd.conf"
Just active :
1)
LoadModule rewrite_module modules/mod_rewrite.so
2)
<Directory />
#AllowOverride none
AllowOverride All
Require all denied
</Directory>
3) I am using "Virtual Host", so in "C:\Apache24\conf\extra" need to change file "httpd-vhosts.conf"
NameVirtualHost *:80
<VirtualHost *:80>
ServerAdmin webmaster#test.com
DocumentRoot "E:/TEST"
<Directory "E:/TEST">
Allow From All
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule . /index.php [L]
</Directory>
ServerName test.com
ServerAlias www.test.com
ErrorLog "logs/test.com-error.log"
CustomLog "logs/test.com-access.log" common
</VirtualHost>
If you are not using "Virtual Host", then I think you need to add some lines to the "Directory" inside of "httpd.conf" !!
<Directory>
Allow From All
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule . /index.php [L]
</Directory>
Use the PHP include function. You can include your MyClass/class.php in you index file. You can then add an htaccess file to restrict files in the MyClass directory from being viewed directly.

Categories