I am trying to rewrite all URIs inside directory /docs to /docs/index.php, using apaches mod_rewrite module.
The URI doesn't exist as a directory structure, which I don't think is necessary when you are rewriting the URI to something that exit.
I am getting the error:
"The requested URL /docs/view/my_doc was not found on this server."
These are my rewrite rules:
RewriteEngine On
RewriteCond %{REQUEST_URI} ^docs/([a-z]+)/([A-Za-z0-9_-]+)/?$
RewriteRule ^(.*)$ http://%{HTTP_HOST}/docs/index.php?action=$1&target=$2 [QSA,L]
I am doing the configuration in httpd.conf. Also, I am using a multi v-host setup.
What's my problem and how do I fix it?
I'm using Apache/httpd on CentOS 6.2
UPDATE
Including my VirtualHost as well:
NameVirtualHost *:80
<VirtualHost *:80>
ServerName www.smicloud.org
ServerAlias *.smicloud.org
VirtualDocumentRoot /var/www/html/www.smicloud.org
ExpiresActive On
ExpiresDefault "access plus 14 days"
# insert logging config, anything else you need..
#Define FileEtag so it doesnt use node
FileETag MTime Size
<Directory /var/www/html/>
Order Allow,Deny
Allow from all
# Get rid of this if you need to allow htaccess files:
AllowOverride None
</Directory>
RewriteEngine On
RewriteRule ^docs/([a-z]+)/([A-Za-z0-9_-]+)/$ /docs/index.php?action=$1&target=$2 [QSA,L]
</VirtualHost>
This should work
RewriteEngine On
RewriteRule ^docs/([a-z]+)/([A-Za-z0-9_-]+)/$ /docs/index.php?action=$1&target=$2 [QSA,L]
Ok, there were a couple of things that I had to do in order to fix it, using #donalds suggestion (which I actually tried before):
1 - I had to have a '?' at the end of the regex of RewriteRule:
^/docs/([a-z]+)/([A-Za-z0-9_-]+)/?$
Then I can chose whether to have a '/' or not at the end of the address.
2 - I had to add a '/' to the beginning of the regex of my RewriteRule. I.e.:
^/docs/([a-z]+)/([A-Za-z0-9_-]+)$
3 - I also had to add the 'http://%{HTTP_HOST}/' part to the new URI:
http://%{HTTP_HOST}/docs/index.php?action=$1&target=$2 [QSA,L]
This is what I ended up with:
RewriteRule ^/docs/([a-z]+)/([A-Za-z0-9_-]+)$
http://%{HTTP_HOST}/docs/index.php?action=$1&target=$2 [QSA,L]
Perhaps why I needed to add the %{HTTP:HOST} was because it's a multi virtual host setup.
Related
Okay I am developing an app with laravel 5.4.
I copied all the files from the laravel installation to a new sub domain, including all the . files that don't copy with a standard copy command.
It easily loads the page without hassle. However the moment any routes are involved it doesnt work, meaning when i click on login, it just says doesnt exist.
I have cleared the cache, in hope that would fix it, however it hasnt.
Please advise of what could be the issue.
Thanks all of you.
//Edit
The subdomain is created on a new virtual host.
File Structure
//Edit
Okay I dont think the error is with laravel at all, i think it is with the virtual hosts file in apache.
The reason for me thinking this is when i change the document root to a working version of laravel it doesnt work either. However the working version of laravel when i use the direct ip address for it then it works.
//Edit
The .htaccess contains
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
RewriteEngine On
# Redirect Trailing Slashes If Not A Folder...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1 [L,R=301]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
# Handle Authorization Header
RewriteCond %{HTTP:Authorization} .
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
</IfModule>
//Edit
The Virtual host file is as follows
<VirtualHost *:80>
ServerAdmin info#1bg.co.za
ServerName thirddoor.livingphate.com
ServerAlias thirddoor.livingphate.com
DocumentRoot /var/www/thirddoor.livingphate.com/public
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
//Edit
Okay, thanks to Digital Ocean, they have helped me and given me more information. The Issue is laravel and the way that laravel works with sub domains.
They have sent me the following links however i have not
http://jpcamara.com/clean-subdomains-in-laravel/
I'm trying to redirect requests for mis-spelt domain names to the same server on the official domain.
My apache configuration looks like:
<VirtualHost *:80>
RewriteEngine on
# Fix domain spellings in host.<backupdomain>
RewriteCond %{HTTP_HOST} !([^.]+).example.com [NC]
RewriteRule ^/(.*) http://%1.example.com/$1 [NC,R,L]
</VirtualHost>
I know I'm close, because the requests to server99.wrongdomain get re-written to .example.com - and I'm expecting it to go to server99.example.com.
Why isn't the regex capture/expansion working correctly here?
P.S. Incredibly annoying that SO is blocking my original examples because they look like links (!)
If you want to match something not followed by something else then you can use Negative lookahead.
RewriteCond %{HTTP_HOST} ^([^.]+)\.(?!example\.com) [NC]
RewriteRule ^/(.*)$ http://%1.example.com/$1 [NC,R,L]
This way, each wrong domain (with server99 for example)
server99.example.co.uk
server99.exampel.com
etc
will redirect to server99.example.com.
That is virtually everything you need in case you only want to redirect misspelled subdomains.
<VirtualHost *:80>
ServerName *.example.com
RedirectMatch 301 /(.*) http://www.example.com/$1
</VirtualHost>
If you want to redirect every request for which there is no uniqe VHost configuration, use the following and make sure it is the very first VHOST configuration loaded by apache
<VirtualHost _default_:80>
RedirectMatch 301 /(.*) http://www.example.com/$1
</VirtualHost>
Of course that only works if the DNS record of the FQDN points to the apache in question.
I am trying to get Subdomains work for my Site using htaccess, For example:
shopping.site.local should lead the user to site.local/shopping-and-fashion keeping the initial subdomain format, means in an internal redirection that the user can't notice.
I have added the following script:
RewriteCond %{HTTP_HOST} ^shopping.site.local$ [OR]
RewriteCond %{HTTP_HOST} ^www.shopping.site.local$
RewriteRule ^(.*)$ http://www.site.local/shopping-and-fashion/$1 [L]
The redirection happens successfully but in an external way, how to hide the redirection to have always the url:
http://shopping.site.local
and the content of:
http://www.site.local/shopping-and-fashion
Did you tried to use [P] instead of [L] ?
The following uses reverse proxying and should give you what you want
<VirtualHost *:80>
ServerName shopping.site.local
ProxyPass / http://www.site.local/shopping-and-fashion/ nocanon
ProxyPassReverse / http://www.site.local/shopping-and-fashion/
</VirtualHost>
You might have to hack some JS and CSS to accommodate for the path changes if the original files are not relative links friendly.
Is there a way to make a rewrite rule to file located in a different than DocumentRoot path? Say I have domain http://test.ldt/ with DocumentRoot /home/test_ltd/ and I want that when a file is requested under static. subdomain (http://stats.test.ldt/) it would look for requested file from another path, say /home/static_files/
I was advised to use mod_alias. However, I am not sure how to make it work when I need it with subdomain.
to cristis:
You are not right. For example if these would be mine httpd rules:
ServerName domain.ltd
ServerAlias www.domain.ltd
DocumentRoot /home/domain_ltd
RewriteEngine on
RewriteCond %{HTTP_HOST} static2.domain.ltd
RewriteRule (.*)$ /home/static_files/$1 [L]
DirectoryIndex index.html index.htm index.php
And client would request for static2.domain.ltd/foo.txt, the file would be searched in /home/domain_ltd/home/static_files/$1
To me, it would make the most sense to just define the subdomain as a VirtualHost that has a DocumentRoot that points where you wanted it to (Although we're moving into ServerFault territory here technically, I guess...)
e.g.:
<VirtualHost *:80>
ServerName static.domain.tld
DocumentRoot /home/static_files/
<Directory /home/static_files>
Options Indexes FollowSymLinks Includes
AllowOverride All
Order allow,deny
Allow from all
</Directory>
</VirtualHost>
But you could do it with a combination of mod_alias and mod_rewrite too, if you wanted. Something like this should work...
In httpd.conf:
Alias /static /home/static_files
In .htaccess (or preferably in httpd.conf in the Directory section for /home/domain_tld):
RewriteEngine On
RewriteCond %{HTTP_HOST} ^static
RewriteRule ^!static - [C]
RewriteRule ^(.*)$ static/$1 [PT]
You can include the full path in the RewriteRule statement. For instance:
RewriteEngine on
RewriteRule ^/~(([a-z])[a-z0-9]+)(.*) /home/$2/$1/.www$3
(taken from http://httpd.apache.org/docs/2.0/misc/rewriteguide.html)
I've been scouring the net and SO and I can't get around or through this problem.
We have a bunch of subdomains and a few dedicated servers. One server does double-triple duty as issue tracking and landing page. Problem is the wildcard landing page doesn't take you to the correct virtual host page sometimes. I've been looking at wildcards but they seem particularly broad.
Our scenario is the following:
-www.askia.com is the main landing site. A non-existing (wildcard) subdomain should always land here.
-dev.askia.com is the technical support and issues site. It has some mod_rewrites for https. It took me a while, but I got it to work and I'd rather not break it.
-www.askia.fr is our french site. Instead of taking you to www.askia.com it takes you to the dev.askia.com.
-www.askia.co.uk should take you to www.askia.com but it goes to dev.askia.com
I'm not entirely sure where I should be trying to fix the solution. Should I do something in the CNAME. In the virtualhosts config file or in the mod_rewrite file.
Try these rules:
RewriteCond %{HTTP_HOST} ^dev\.
RewriteCond %{HTTP_HOST} !^dev\.askia\.com$
RewriteRule ^ http://dev.askia.com%{REQUEST_URI} [L,R=301]
RewriteCond %{HTTP_HOST} !^www\.askia\.com$
RewriteCond %{HTTP_HOST} !^dev\.askia\.com$
RewriteRule ^ http://www.askia.com%{REQUEST_URI} [L,R=301]
The first rule redirects every request to a host starting with dev. but not dev.askia.com to www.askia.com. And the second rule redirect requests to a host other than www.askia.com and dev.askia.com to www.askia.com. So every request should either go to dev.askia.com or www.askia.com.
When using Virtual Hosts in Apache the first hosted listed will always be the default for non-matches.
#default vhost
# any non-matches will land here
<VirtualHost _default_:80>
ServerName www.askia.com:80
DocumentRoot /path/to/site
ErrorLog /path/ti/sites/logs/error_log
</VirtualHost>
# vhost #2
<VirtualHost _dev_Site_:443>
ServerName dev.askia.com:443
DocumentRoot /path/to/dev/site
ErrorLog /path/to/dev/sites/logs/error_log
#ssl details
SSLEngine on
SSLCipherSuite HIGH:MEDIUM
SSLCertificateFile /location/securti.crt
SSLCertificateKeyFile /location/securti.key
#any rewrite rules to apply only to this (default) domain
# force SSL for instance..
RewriteRule .* - [F]
RewriteCond %{SERVER_PORT} !^443$
RewriteRule (.*) https://dev.askia.com/
</VirtualHost>
#etc, etc