I am putting all of my website's content (aside from the home page) into a "content" folder at the root of the site. This is Apache 2.4.25.
I want http://www.example.com to serve the DirectoryIndex (i.e. index.html) at C:/DocumentRoot/. The following works fine for that.
<Directory "C:/DocumentRoot/website">
Options None
AllowOverride None
Require all granted
</Directory>
I then want to have http://www.example.com/anything1/anything2 serve the DirectoryIndex at C:/DocumentRoot/content/anything1/anything2. After adding the following, accessing http://www.example.com gives a Forbidden error, though the AliasMatch works.
AliasMatch "^/(.+)$" "C:/DocumentRoot/website/content/$1"
<Directory "C:/DocumentRoot/website/content/">
Require all granted
</Directory>
Any idea what's happening or have a better/working alternative?
In this case, mod_rewrite is a little easier to read and later extend then a negative assertion regex in the AliasMatch.
RewriteEngine ON
RewriteCond %{REQUEST_URI} !^/content
RewriteRule ^/(.+) "C:/DocumentRoot/website/content/$1"
A change to the AliasMatch so it ignores the DirectoryIndex (index.html) allows it to work as expected.
AliasMatch "^/(?!index.html)(.+)$" "C:/DocumentRoot/website/content/$1"
(?!index.html) ensures that the implicit index.html is not matched. The (.+) picks up anything else and pulls it from the content folder.
Related
I have a quite simple rewrite rule, it works meaning the rewrite works but the parameter does not reach the destination page.
Each page has a section, title and a id.
The url has dashes in the name and I use a underscore separator to separate the id.
The section name is hypnose
The title is Hoe-werkt-hypnose
The id is 4
The rewrite rule:
RewriteRule ^hypnose/([^/.]+)_([^/.]+).php$ hypnose.php?title=$1&id=$2 [L]
The url that i'm using
hypnose/Hoe-werkt-hypnose_4.php
You need to turn MultiViews option off:
Options -MultiViews
RewriteEngine On
RewriteRule ^hypnose/([^/_]+)_([^/.]+)\.php$ hypnose.php?title=$1&id=$2 [L,QSA,NC]
Option MultiViews is used by Apache's content negotiation module that runs before mod_rewrite and makes Apache server match extensions of files. So /file can be in URL but it will serve /file.php.
I'm really not into regular expression and I have trouble to set a rule for my website.
I have two index files, named index-pc.php and a second named index-mob.php, and I want to be able to hide the -pc/-mob part of the filename. Which syntax I should use to do so?
My current rule is:
RewriteEngine On
RewriteRule ^index-(pc|mob)\.php$ index.php [L]
But this rule is not working. What I'm missing?
Thank you.
The regex seems to be correct. Did you enabled .htaccess already? If not, open your httpd.conf and change
<Directory />
....
AllowOverride None
</Directory>
to
<Directory />
....
AllowOverride All
</Directory>
I have the following .htaccess file on our xxx.yyy.edu site, in the root.
<FilesMatch "(.+)\.php">
AuthType shibboleth
ShibRequestSetting requireSession 1
ShibUseHeaders On
Require valid-user
ShibRequestSetting redirectToSSL 443
</FilesMatch>
This should only match files that end in .php, and match all files that end in .php, then force authentication. That part of it appears to work fine.
However, it also matches the root url, http://xxx.yyy.edu and http://xxx.yyy.edu/. Which isn’t a good thing, since it is forcing the root of the site to be authenticated.
It does not match xxx.yyy.edu/index.htm, or any other url that has anything after the domain name, like xxx.yyy.edu/students.htm or even xxx.yyy.edu/x
I’ve tested it in www.regexr.com, and it does not match the root url there.
Any thoughts/suggestions appreciated.
You are getting authentication dialog because on your landing page default page is set to index.php.
Have this FilesMatch with negative lookahead to avoid index.php from authentication:
<FilesMatch "^(?!index\.php$).+?\.php$">
AuthType shibboleth
ShibRequestSetting requireSession 1
ShibUseHeaders On
Require valid-user
ShibRequestSetting redirectToSSL 443
</FilesMatch>
Sounds like perhaps it is matching the newline with the "dot" (a la XRegExp). Try something like \w+:[\d\w\s\./]+\.php.
I wanted to serve .xhtml files as
application/xhtml+xml if the browser says that it accepts it.
text/html otherwise
I tried doing it with mod_rewrite but it didn't work with Options -FollowSymLinks (see Why I do I get 403 Forbidden when viewing files affected by Apache RewriteRule if I have `Options -FollowSymLinks`?).
Then, I tried
<Files "*.xhtml">
<If "%{HTTP:Accept} !~ /application\/xhtml\+xml/">
ForceType text/html
</If>
</Files>
But I get a syntax error: Failed to compile regular expression.
Meanwhile, I use this code...
<Files "*.xhtml">
<If "%{HTTP:Accept} !~ /xhtml\+xml/">
ForceType text/html
</If>
</Files>
... which works, but I want to match the correct MIME type.
You could use an escape code like \x2F instead of the /.
It looks like improving this is still under construction as of Apache 2.4. Apache team member "covener" recommends m#regexp# instead.
So your code would look like this...
<If "%{HTTP:Accept} !~ m#application/xhtml\+xml#">
My /train directory is aliased to a script in httpd.conf by:
WSGIScriptAlias /train /some-path/../django.wsgi
And it works well, except for one problem. If a user goes to /train (with no trailing slash) it will not redirect him to /train/, but will just give him the right page. This is a problem because this way the relative links on this page lead to the wrong place when no trailing slash was used to access it.
How can this be worked out?
Thanks.
I'm using something like this for redirecting /train to /train/, what I do is redirecting all the URL than doesn't end with / to /train/.
<Location "/train">
Order deny,allow
Allow from all
RewriteEngine on
RewriteRule !^.*/$ /train/ [R]
</Location>
WSGIScriptAlias /train /some-path/../django.wsgi
If you just need to redirect from /train to /train/ and not from every subdirectory without a trailing slash, then there's a simpler solution using the RedirectMatch directive:
RedirectMatch ^/train$ /train/
Set your urlconf to accept train/ as valid instead, then make train lead to a generic redirect to /train/.