I need some help with URL rewriting. The case is similar with this one.
I have a working Zend Framework site. Now I must add a blog in Wordpress (also working). I've chosen not to indulge in ZF controller-/action-/route-making; I've seen a couple of tutorials about this and I consider them too much for a "plain" redirection. Now, about that "redirection"...
This is how it should look like:
www.site.com (points to /var/www/zf)
www.site.com/blog (points to /var/www/wp)
I know that I should stop www.site.com/blog to enter ZF's innards and I'm currently doing this with RewriteRule ^blog - [NC,L] in its .htaccess, but that's about it. As #jason said, "just pass it through to Wordpress", but I don't know how exactly to do that.
Related question:
I never tried it, but does Apache support this in two different vhosts?
ServerName www.site.com (vhost for ZF site)
ServerName www.site.com/blog (vhost for WP site)
www.site.com (points to /var/www/zf)
www.site.com/blog (points to /var/www/wp)
The easiset way to achive this, where you want a sub-url to point outside the VirtualHost's DocumentRoot directory, is to create an Alias...
Inside the VirtualHost block add:
Alias /blog /var/www/wp
<Directory /var/www/wp>
Options All
AllowOverride All
order allow,deny
allow from all
</Directory>
*This assumes you have PHP enabled in some way for that directory.
The ServerName trick will not work: you can not have path names in a ServerName directive.
For mod_rewrite you should be able to get away with something like this:
RewriteEngine on
RewriteBase /
RewriteRule ^blog/(.*)$ wp/$1 [L]
RewriteRule ^(.*)$ zf/$1 [L]
Related
Now I've written dozens of redirects in my time, some with tricky regex, some more tame, but today, the very simplest redirect is stumping me on a CentOS server, running Apache 2.2.3.
All I'd like to do is redirect every single request on an old domain, regardless of path and query string, to the front page only of a new site. This is why, for example, a mod_alias Redirect directive isn't appropriate, since it appends the path to the new address.
In an Apache conf file, where the virtual server is defined, I now have
<VirtualHost THE.IP.ADDRESS:80>
DocumentRoot "/var/www/html/SITE_ROOT"
ServerName OLD_DOMAIN.com
<Directory "/var/www/html/SITE_ROOT">
Options FollowSymLinks
RewriteEngine On
RewriteRule ^$ https://NEW_DOMAIN [R=301,L]
AllowOverride None
</Directory>
</VirtualHost>
While the redirect to https://NEW_DOMAIN occurs as expected, the path of the original request is always appended, leading to 404 errors on the new site.
For example, visiting http://OLD_DOMAIN.com/asdf
redirects to https://NEW_DOMAIN.com/asdf
...when I'd actually want to arrive at https://NEW_DOMAIN.com/
Why is the path being appended, even though I'm not collecting a pattern match, and am not specifying such a match in the destination?
There are plenty of answers like this on SO already:
Apache redirect to a clean URL
https://stackoverflow.com/a/11590814/1738274
But I can't find a discrepancy comparing these solutions against my own configuration. Any ideas?
RewriteRule ^(.*)$ https://NEW_DOMAIN [R=301,NC,L] should work. I have tested with various URLs and it always redirects to https://NEW_DOMAIN
My config structure looks a bit different:
<VirtualHost *:80>
ServerName OLD_DOMAIN.com
DocumentRoot "C:\Program Files (x86)\Apache Software Foundation\Apache2.2\htdocs"
<IfModule mod_rewrite.c>
RewriteEngine on
Options FollowSymLinks
RewriteRule ^(.*)$ https://NEW_DOMAIN [R=301,NC,L]
.......
.......
I have a .htaccess file which (among other things) creates pretty urls. Here's a sample of part of the file:
RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
...
# Pretty urls
RewriteRule ^home(\d+)*$ ./index.html
RewriteRule ^contact(\d+)*$ ./contact.html
RewriteRule ^enquire(\d+)*$ ./enquire.html
RewriteRule ^terms(\d+)*$ ./terms.html
RewriteRule ^about(\d+)*$ ./about.html
RewriteRule ^owners(\d+)*$ ./owners.html
I am currently moving the site to new hosting on digitalocean. When I test the site out by browsing the IP e.g.
111.222.333.444/html_docs/contact
the .htaccess file is working perfectly. Rewrite rules are on and the site properly rewrites the url and serves (in this case, contact.html) to the browser.
When I change the nameservers and browse the site by domain after propogation e.g.
mydomain.com/contact
the .htacces file fails to do the redirect, at least, it seems like it's not being seen at all. This is weird to me since it works when I'm browsing by ip. I purposely put some rubbish into the .htaccess to test if it was being picked up. Again, if I browse via the ip I get a 500 which I would expect, but when browsing by domain name I do not get an error but the rewrites don't work.
I'm banging my head at this stage and can't figure it out. Any help appreciated,
many thanks,
Wittner
Ok, this was less of a lack of knowledge about how .htaccess works and more to do with not configuring my vhosts settings properly.
<VirtualHost *:80>
ServerName mydomain.com
ServerAlias www.mydomain.com
ServerAdmin me#mydomain.com
DocumentRoot "/var/www/mydomain.com/html_docs/"
<Directory "/var/www/mydomain.com/html_docs/"> <-- Problem was here
Options Indexes FollowSymLinks
AllowOverride All
Order allow,deny
Allow from All
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
Turns out that in the tag I had left out the path to my domain root. So when I directly browsed the site via the IP the system was able to figure it out, but no while browsing. All my .htaccess stuff is now working. Thanks to all who looked and anyone who replied. Live and learn I guess :-)
cheers,
Wittner
I've read a tonne of questions on this and tried a few solutions, but I'm just not getting the results I want.
I'm currently using:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^cp\.example\.net
RewriteRule ^(.*)$ http://example.net/gamepanel/public/$1 [L,NC,QSA]
Which doesn't quite work as intended. When I navigate to cp.example.net I am redirected to http://example.net/gamepanel/public/
What I want to have happen is for cp.example.net to show the content at http://example.net/gamepanel/public, whilst showing the subdomain in the URL.
Also cp.example.net/user should show content from example.net/gamepanel/public/user
Some help on achieving this would be much appreciated!
Turns out I was a little off the mark on what I needed to do and that .htaccess wasn't the best way to do this.
Adding this in my apache files solved it
<VirtualHost *:80>
ServerName cp.example.net
DocumentRoot /var/www/gamepanel/public
<Directory /var/www/gamepanel/public>
Options -Indexes
</Directory>
</VirtualHost>
I'm currently in the process of converting an old apache vhost, where one is currently created for each new release, to a dynamic host that can handle all the subdomains so we don't have to create a new one all the time. I need some help! We're using apache v2.2.
The Goal
Dynamic all the things. To have a single virtual host that handles all redirects for a specific set of subdomains. The url is below, note that sub1 and branch are dynamic and could be anything.
sub1.branch.sandbox.domain.com
The directory structure for this is as follows:
/var/www/vhosts/branch/sub1.branch.sandbox.domain.com
As you can see above, the directory structure has the branch as a sub-directory before the full url is the name of another sub-directory.
Also, /images/ in the url needs to forward to shared-httpdocs/images for each domain.
The vhost I have so far
<VirtualHost *:80>
ServerName branch.sandbox.domain.com
ServerAlias *.branch.sandbox.domain.com
VirtualDocumentRoot /var/www/vhosts/branch/%0
Options Indexes FollowSymLinks
# Rewrite Engine Stuff Here
RewriteEngine On
DirectoryIndex index.php
# Assets needs to be forwarded - currently not working
RewriteCond %{REQUEST_URI} ^/(css|js|images)/.*
RewriteRule .*$ /var/www/vhosts/%0/shared-httpdocs/%1$ [L]
# The HTTP dispatcher - currently not working
RewriteCond %{HTTP_HOST} ^(.*)\.branch\.sandbox\.domain\.com [NC]
RewriteRule ^(.*)$ /var/www/vhosts/%1/applications/portal/dispatchers/http.php [L,QSA,NS,NE,NC]
</VirtualHost>
The old host I'm trying to copy
This is the old vhost I'm trying to convert from. It's horrible, messy, and our ops has to create a new DNS entry every time. What a joke! I need to sort this out...
<VirtualHost *:80>
# Notice how the stupid convention below will require a new DNS entry each time?
ServerName sandbox.branch.domain.com
ServerAlias sandbox.api.branch.domain.com
DocumentRoot /var/www/vhosts/sandbox.branch.domain.com/applications/portal/httpdocs
<Directory "/var/www/vhosts/sandbox.branch.domain.com/applications/portal/httpdocs">
allow from all
order allow,deny
# Enables .htaccess files for this site
#AllowOverride All
RewriteEngine On
# Rewrite all non-static requests to go through the webapp
RewriteCond %{REQUEST_URI} ^/(css|js|images)/.*
RewriteRule .* - [L]
# Rewrite everything else to go through the webapp
RewriteRule ^(.*)$ /dispatchers/http.php [QSA,L]
</Directory>
<Directory "/var/www/vhosts/sandbox.branch.domain.com/applications/portal/dispatchers">
allow from all
</Directory>
# Allow us to rewrite to the webapp without it being in the webroot
Alias /dispatchers /var/www/vhosts/sandbox.branch.domain.com/applications/portal/dispatchers
# Get shared/ to point to the shared static resources
Alias /shared /var/www/vhosts/sandbox.branch.domain.com/shared-httpdocs
</VirtualHost>
A new DNS entry is required each time we have a new branch, so I'm trying to mitigate this by providing a dynamic subdomain vhost (see the vhost I have so far). I've gone from not even being able to match /images/ in the url to a permanent redirect loop.
How can I achieve my goal? I know it's a little complex. If I can't do it, I'll just have to write a script that will generate a new vhost each time but a dynamic one that 'just works' would be fantastic. I've put two days into this so far, I'm no sysadmin. Your help would be greatly appreciated.
Resources I have been using:
mod_rewrite official docs - Shows the basics like things on conditions with REWRITE_COND
Sub domain rewriting - A question on subdomain rewriting
Asset rewriting - Another question on rewriting things like images / css / js, which doesn't seem to work for me
It's not a complete answer, but is too long for comment.
The %0 (%0 to %9) in a rewrite rule are back references to captures in the last RewriteCond. It seems to me you wanted instead the host name. Also it seems you miss the "branch" part of the path. In the Asset's rewrite you also throw away the filename part.
# Assets needs to be forwarded - currently not working
RewriteCond %{REQUEST_URI} ^/(css|js|images)/(.*)
RewriteRule .*$ /var/www/vhosts/branch/%{HTTP_HOST}/shared-httpdocs/%1/%2$ [L]
# The HTTP dispatcher - currently not working
RewriteCond %{HTTP_HOST} ^(.*)\.branch\.sandbox\.domain\.com [NC]
RewriteRule ^(.*)$ /var/www/vhosts/branch/%{HTTP_HOST}/applications/portal/dispatchers/http.php [L,QSA,NS,NE,NC]
You can get debugging help also from mod_rewrite dedicated logging with RewriteLog and RewriteLogLevel directives.
Hope this will bring you further.
I just made a subdomain on my webside mainly becouse i want a spesific address to it.
Lets say my website is : http://website.com
And my subdomain is http://sub.website.com with its main folder in /home/username/public_html/sub/
When i enter the subdomain address the address suddently changes to http//website.com/sub/
how can i keep the subdomain address?
EDIT for Dennis:
i have a rule that directs all http://www.website.com to http://website.com
I use
RewriteEngine on
Redirect to non-www
RewriteCond %{HTTP_HOST} !^(website\.com)?$
RewriteRule (.*) http://website.com/$1 [R=301,L]
even if i comment out this the address still changes to
http://website.com/sub/
Best of regards,
Alexander
I would imagine something like this would work (not tested):
Options FollowSymLinks
RewriteEngine On
RewriteCond %{HTTP_HOST} !^website\.com
RewriteCond %{HTTP_HOST} ^([^.]+)
RewriteRule \?cat_id=([0-9]+)&photo_id=([0-9]+) http://website.com/$1/$2/
RewriteRule ^/(.*)/(.*)/ /%1.php?cat_id=$1&photo_id=$2 [P]
Note: according to the documentation, Options FollowSymLinks needs to
be an option you can override to get the rewrite to work.
The first RewriteCond will ensure the "webiste.com" host is not rewritten (but
domains like sub.website.com will). The second one matches anything
up to the first dot of the hostname. Both of these must succeed and if so the host
part is accessible in %1.
The first RewriteRule rewrites the args to the php script to the directory paths
(a change seen on URL on the browser). The next RewriteRule uses the [P]
flag to proxy any requests of the form /xx/yy/ to %1.php (our remembered host name)
with the args passed to the php script.
As I've stated, this is not tested, but hopefully it will get you "in the right direction".
EDIT: removed the [R] from the first rewrite rule.
Another solution is to have a Virtualhost dedicated for your subdomain (that should already be the case, if not you'll get a lot of problems) and to make this VirtualHost ignore .htaccess instructions of the master domain. You should really try to keep your subdomain configuration independant of the master domain domain configuration.
Let's say your documentRoot in the subdomain VH is /home/username/public_html/sub/, when Apache serve the index.php or index.html file or anything else in this directoy it performs a recursive search of all .htaccess in /home/username/public_html/sub/, /home/username/public_html/, /home/username/, /home/ and /. So the rules defined in your master domain (I think it's in /home/username/public_html/) are applied.
You can tell this subdomain VirtualHost to ignore all .htaccess files which are before the VH DocumentRoot by adding:
<Directory />
AllowOverride None
</Directory>
<Directory /home/username/public_html/sub/>
AllowOverride All
</Directory>
You could even remove the AllowOverride All, remove all .htaccess and put the things you have in the .htaccess of your subdomain (if you have one) directly in the <Directory /home/username/public_html/sub/> section, same effect without having Apache searching for configuration parts on the filesystem (so faster).