LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Server
User Name
Password
Linux - Server This forum is for the discussion of Linux Software used in a server related context.

Notices


Reply
  Search this Thread
Old 04-23-2015, 08:47 AM   #1
ddench
LQ Newbie
 
Registered: Sep 2012
Location: London/Europe
Distribution: openSUSE CENTOS
Posts: 6

Rep: Reputation: Disabled
Help with Apache httpd 2.4 ModRewrite rules


On my apache 2.4 server i have the following folder structure:

var/www/html/
...1/
...3/
.....appA/
.....appB/
.....appC/
...appA/
...appB/
...appC/

I have two domains set up with 2 virtual hosts files that both are set to redirect http to https.
The domain document roots point to the following folders

...domain1 html/1/
...domain2 html/

domain2 has its main site at html/3/ with sub pages that match the title of the app folders.
How can I use RewriteRules to map

www.domain2.com to www.domain2.com/3
and
www.domain2.com/3/appA to www.domain2.com/3/appA
but
www.domain2.com/appA stays as it is

Everytime i attempt a RewriteCond and/or Rule either apache fails to load due to an error or I can only access domain2.com/3/appA but not domain2.com/appA here are some of the current settings I have tried:

<VirtualHost *:443>
DocumentRoot "/var/www/html"
RewriteEngine On
# RewriteCond {REQUEST_URI} /appA
# RewriteRule {REQUEST_URI} /appA
# RewriteRule ^/$ /3
# RewriteRule %{REQUEST_URI} /3
RewriteCond %{HTTP_HOST} ^domain2.com$
RewriteRule ^$ https://www.domain2.com/3
ServerName https://www.domain2.com
ServerAlias www.domain2.com domain2.com
<Directory "/var/www/html">
Options All
AllowOverride All
Require all Granted
</Directory>


Thanks in advance for any help

Last edited by ddench; 04-23-2015 at 08:50 AM.
 
Old 04-23-2015, 12:20 PM   #2
bathory
LQ Guru
 
Registered: Jun 2004
Location: Piraeus
Distribution: Slackware
Posts: 13,163
Blog Entries: 1

Rep: Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032
Hi,

You have some errors in the https vhost definition:
The ServerName is wrong (use of https in front of the hostname).
The RewriteCond and RewriteRule are needless as you are already in the SSL vhost.

Anyway you may try this and see if it works:
Code:
<VirtualHost *:443>
DocumentRoot "/var/www/html"

RewriteEngine On
RewriteCond %{REQUEST_URI} !^/appA
RewriteCond %{REQUEST_URI} !^/3
RewriteRule (.*) /3/$1

ServerName www.domain2.com
ServerAlias www.domain2.com domain2.com
<Directory "/var/www/html">
Options All
AllowOverride All
Require all Granted
</Directory>
</VirtualHost>
 
Old 04-25-2015, 08:13 AM   #3
ddench
LQ Newbie
 
Registered: Sep 2012
Location: London/Europe
Distribution: openSUSE CENTOS
Posts: 6

Original Poster
Rep: Reputation: Disabled
Bathory, I believe it was you that came to my help the last time I posted, so thank you again!

Thank you for pointing out the unnecessary https:

With your changes applied I have been able to get a lot closer to what I have been hoping for, but not quite there. For clarity /3 is the root folder for a drupal install.
With your changes I was able to access the 'appA' directories correctly and domain2.com correctly loaded the Drupal landing page. However the pages that sit within the main part of the site:

www.domain2.com/3/appA

are not accessible. Instead they direct to domain2.com/appA (or are not found - ie domain2.com/post1 should be domain2.com/3/post1)

I navigate to www.domain2.com and am greeted with the domain2.com/index.php landing page of the Drupal install.
But if i select the link that should direct to 3/appA it instead goes to domain2.com/appA

What I think I need is to be redirected (as opposed to rewritten?) to

www.domain2.com/3 when asking for www.domain2.com

Then all subsequent requests would be appropriately sent to .com/3/...

My current config is as follows:

RewriteCond %{REQUEST_URI} !^/appA
RewriteCond %{REQUEST_URI} !^/appB
RewriteCond %{REQUEST_URI} !^/3
RewriteRule /(.*) /3/$1 [L]

This allows me to navigate to the .com/appA directory correctly and if I manually type domain2.com/3 to then navigate to all the Drupal pages correctly.
However www.domain2.com now loads the drupal template but the url called is

http://www.domain2.com/index.php/ind....php/index.php

This is slightly better - but obviously I would like the correct page to load! I wonder if an internal .htaccess file on the Drupal install is what is causing the multiple index.php redirects?

Also, is it possible to 'OR' the first RewriteCond like so:

!^/appA|!^/appB|!^/appC (this didn't work, but as I understand it, | is the OR operator in PECL regex.)


Thanks once again for your help - it is greatly appreciated.
 
Old 04-25-2015, 12:14 PM   #4
bathory
LQ Guru
 
Registered: Jun 2004
Location: Piraeus
Distribution: Slackware
Posts: 13,163
Blog Entries: 1

Rep: Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032
Quote:
For clarity /3 is the root folder for a drupal install.
So why don't you change the docroot to /var/www/html/3? This way you won't need all this rewrite stuff


Quote:
Also, is it possible to 'OR' the first RewriteCond like so:

!^/appA|!^/appB|!^/appC (this didn't work, but as I understand it, | is the OR operator in PECL regex.)
You can use the [OR] flag:
Code:
RewriteCond %{REQUEST_URI} !^/appA [OR]
RewriteCond %{REQUEST_URI} !^/appB [OR]
RewriteCond %{REQUEST_URI} !^/3
RewriteRule /(.*) /3/$1 [L]
 
Old 04-25-2015, 12:36 PM   #5
ddench
LQ Newbie
 
Registered: Sep 2012
Location: London/Europe
Distribution: openSUSE CENTOS
Posts: 6

Original Poster
Rep: Reputation: Disabled
Hi Bathory, thank you for your prompt response.

If I were to change the root directory to /var/www/html/3 then how would one navigate to

domain2.com/appA or /var/www/html/appA/

It would of course work for the Drupal install and all those pages

domain2.com/home /var/www/html/3/index.php
domain2.com/appA /var/www/html/3/appA
domain2.com/post1 /var/www/html/3/post1

That was one of the first things I tried, but it soon became clear that - well in my seemingly limited understanding at least - it wouldn't work that way round.
By changing the docroot there would then be no way at all to navigate to the /var/www/html/appA/... pages? Is this just a fact of life, in this instance?

With regards the [OR] I meant is there a way to condense multiple similar rewritecond's onto one line - it isn't really important, just a suggestion from a friend.

Thanks again. Perhaps I need to think of a different way to approach this. I would essentially like to be able to navigate to those /html/appA folders whilst still using the domain2.com url. Perhaps subdomains would be a more appropriate solution appA.domain2.com or perhaps they simply shouldn't have the same naming structure!
 
  


Reply

Tags
apache, httpd, modrewrite, rewritecond



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
After disabling SSLv3 Apache Jmeter not able to connect to Apache httpd Iyyappan Linux - Server 3 01-19-2015 09:30 AM
modrewrite enabled but not working?? simonm Linux - Newbie 1 10-12-2008 12:59 PM
Failed to start apache :Starting httpd: Syntax error on line 1027 of /etc/httpd/conf/ payjoe Linux - Newbie 3 09-21-2007 07:24 AM
Apache :: ModRewrite ? c0nsur Linux - Software 5 03-31-2006 09:38 PM
httpd chokes on ScriptAlias line in Apache httpd.conf lhoff Linux - Software 1 07-14-2003 10:32 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Server

All times are GMT -5. The time now is 06:14 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration