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/.
Related
My goal is to deny access to all dot-files e.g .htaccess, .env and send back a 404, but allow the letsencrypt-folder .well-known to be accessed
RewriteEngine On
RewriteRule "(^|/)\.(?!well-known)" - [F]
RedirectMatch 404 /\..*$
Any hint on how to achieve this is highly appreciated
Best endo
Try the following instead:
RewriteEngine On
RewriteRule (?:^|/)\.[^/]+$ - [R=404]
This will serve a 404 for any file (or rather, last URL-path segment) that starts with a dot. But it will permit .well-known/ - since this is a directory and so is also suffixed by at least a slash + filename.
UPDATE: Modified regex so that it matches the dot at the start of the last path-segment, rather than anywhere in the last path-segment!
Note that the F flag responds with a 403 Forbidden, not a 404 as requested.
Alternatively, you can use a <Files> (or <FilesMatch>) container, which only matches "files". For example:
<Files ".*">
# 404 Not Found
Redirect 404 /
# OR... 403 Forbidden
#Require all denied
</Files>
Although this does also block a request for /.well-known (no trailing slash) - although that's not strictly a valid request anyway.
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.
I have urls like www.example.com/de/something and I need to redirect to www.example.com everything that starts with /de/.
At the moment I have done this
redirect 301 /de http://example.com
and it redirect all links but just removing /de part and result is www.example.com/something.
How to fix this?
Thanks
redirect directive matchs rest of the uri and appends it to the target, you can use RedirectMatch to redirect a specific uri
redirectMatch 301 ^/de/? http://example.com
If you want /de/ in the target you should have specified so, becasue Redirect will include in the target everything "after" what you have matched.
For a different virtualhost as the destination you want this:
Redirect 301 /de/ http://example.com/de/
or
Redirect 301 /de http://example.com/de
If what you want is redirect inside the same virtualhost /de to /, then use a negative lookahead.
RedirectMatch ^(?!de) http://example.com/
If the context is .htaccess, for virtualhost It would be:
RedirectMatch ^/(?!de) http://example.com/
Note: I use /de/ originally because that's what you describe in your question, and also I match slashes in the target. Both source and target without slashes would be fine too for cases like /desomething or /de/something. In any case, always match slashes or the lack of them.
Note2: Do not use .htaccess to redirect unless you are not the admin of the site. It just complicates things and adds unnecessary overhead since the file/s need to be checked a number of times per hit.
Iam using apache mod_rewrite to redirect some URL. I been able to some of the URLS but none of my rules are able to caprure this
http://192.23.70.11/mome/GIS%20Endo/%2Fhome%2FGIS%2520Endo%2F
and redirect it to
http://192.23.70.11/mome/GIS%20Endo/
You can use this rule as your first rule in site root .htaccess or Apache config:
# this goes in httpd.conf
AllowEncodedSlashes On
RewriteEngine On
RewriteRule ^(/?mome/[^/]+)/.+$ /$1 [L,NC,R=301,NE]
This will match any URI starting with /mome and will truncate anything after 2nd path component.
I'm having a strange behaviour with Apache's LocationMatch directive when there are extra slashes at the beginning of the URL. According to the Apache docs if I'm reading it right this should work:
<LocationMatch ^/appcontext/(a|b)>
SetHandler weblogic-handler
WebLogicCluster apphost01:xxxx,apphost02:xxxx
WLProxySSL ON
</LocationMatch>
However if I type the following URL it is also being forwarded to the backend hosts:
https:// <hostname:port> ////////appcontext/a/
In the Apache docs it clearly states that it should apply the directive only for /appcontext/a/, unless I'm missing something with the regex or there is some issue with the mod_wl plug-in
http://httpd.apache.org/docs/2.2/mod/core.html
"For example, <LocationMatch ^/abc> would match the request URL /abc but not the request URL //abc"
Does anyone have any suggestion to achieve this?
Yes Apache does collapse extra slashes in rewrite rule. You can use THE_REQUEST variable to match and remove extra slashes. Place this in root .htaccess:
RewriteEngine On
RewriteCond %{REQUEST_URI} ^(.*?)//+(.*)$
RewriteRule ^ %1/%2 [R=302,L,NE]