AEM Dispatcher vhost settings with rewrite rule is not working - apache

I am unable to get the rewrite working on Dispatcher module of AEM.
Following is what's happening with my configuration:
Dispatcher configuration:
Snippet of my httpd.conf
LoadModule dispatcher_module modules/mod_dispatcher.so
<IfModule disp_apache2.c>
DispatcherConfig conf/dispatcher.any
DispatcherLog logs/dispatcher.log
DispatcherLogLevel 3
DispatcherNoServerHeader 0
DispatcherDeclineRoot 0
DispatcherUseProcessedURL 0
DispatcherPassError 0
</IfModule>
<VirtualHost *:80>
ServerName mobile.example.com.au
DocumentRoot "/var/www/html/content/mobile"
RewriteEngine on
RewriteRule ^/(.*)\.html$ /content/mobile/$1.html [PT]
<Directory "/var/www/html/content/mobile">
<IfModule disp_apache2.c>
SetHandler dispatcher-handler
ModMimeUsePathInfo On
</IfModule>
Options FollowSymLinks
AllowOverride None
# AddType text/html .html
# AddOutputFilter INCLUDES .html
# Options +Includes
</Directory>
</VirtualHost>
......
Now, if I try to access the mobile.example.com.au/home.html, it shows up this 404 and says no content:
Not Found
The requested URL /content/mobile/home.html was
not found on this server.
Though I am able to hit the page directly by using the full path:
http://mobile.example.com.au/content/mobile/home.html and the page is rendered.
What am I missing here? Is it something to do with the Document root?
Also, how do we set the default landing page for mobile.example.com.au to show up home.html.

Can you please check rewrite logs to see how request is flowing?
Also, to set default landing page, you can use following rule
RewriteRule ^/$ /content/mobile/home.html [PT,L]

I believe you need to change this line:
DispatcherUseProcessedURL 0
to
DispatcherUseProcessedURL 1
... so that Dispatcher uses the rewritten URL for filter checks, cache structure, and the request to AEM. You'll also need to add the RewriteRule as Mohit suggested, probably as the first rule - but only if you want / to show the /home.html page.
See also: Adobe documentation on Dispatcher configuration

Related

allow override for path in a virtual host

There are some PHP files on an external drive which is mapped to the Z: drive on a Windows 10 machine. This code is in the httpd-vhosts.conf Apache file:
<VirtualHost *:8080>
DocumentRoot "Z:/files/xampp/htdocs"
<Directory "Z:/files/xampp/htdocs">
Options Indexes
Require all granted
</Directory>
</VirtualHost>
The task at hand is to allow .htaccess overrides for a file path within the path shown above. In the httpd.conf file the rewrite module is enabled. That line looks like this: LoadModule rewrite_module modules/mod_rewrite.so. I tried adding this code to the httpd.conf file:
<Directory "Z:/files/xampp/htdocs/miscellaneous/WebServiceExamples">
AllowOverride All
</Directory>
The Apache web server was stopped and restarted after making these changes. The code in .htaccess looks like this:
<IfModule mod_rewrite.c>
# turn rewrite engine on
Options +FollowSymlinks
RewriteEngine on
# map neat URL to internal URL
RewriteRule ^mobile/list/$ RestController.php?view=all [nc,qsa]
RewriteRule ^mobile/show/([0-9]+)/$ RestController.php?view=single&id=$1 [nc,qsa]
</IfModule>
However, when a URL that should be handled by the rewrite rule in the .htaccess file is entered in the Chrome address bar the result is an Object not found! Error 404. Where did I go astray?
EDIT:
Another thing I tried was putting httpd.conf back to its original state, then modifying httpd-vhosts.conf as follows:
<VirtualHost *:8080>
DocumentRoot "Z:/files/xampp/htdocs"
<Directory "Z:/files/xampp/htdocs">
Options Indexes
Require all granted
</Directory>
<Directory "Z:/files/xampp/htdocs/miscellaneous/WebServiceExamples">
AllowOverride All
</Directory>
</VirtualHost>
After stopping and restarting the Apache web server, the result is still the same as before:
Object not found! Error 404.
Figured this out. The configuration files were left in the state as described in the Edit section in the original post. The Troubleshooting section in this link was a helpful guide for determining that the configuration files were set up correctly.
This was entered as the URL in Chrome's address bar: http://localhost:8080/miscellaneous/WebServiceExamples/SimpleRestfulWebService/mobile/list. Notice there is no trailing forward slash. The rewrite rule was in .htaccess was written in such a way that made a trailing forward slash mandatory. When the rewrite rule lines were modified to make the trailing slash optional, then it worked fine. Here are the modified rewrite rules:
RewriteRule ^mobile/list/?$ RestController.php?view=all [nc,qsa]
RewriteRule ^mobile/show/([0-9]+)/?$ RestController.php?view=single&id=$1 [nc,qsa]

Split DocumentRoot using Apache Alias per request path

I'm trying to slowly take over an existing website, route per route. I found Apache's Alias(Match) which seems to allow me to set a different (content) document root per request. But I'm failing with trailing slashes and more complex paths.
My goal now is to have the old website serve everything as it is used to. And to have a new website, serving a first specific request, say /foo and /foo/*.
I have my vhost setup like this:
<VirtualHost *:80>
ServerName example.com
UseCanonicalName on
AliasMatch ^/foo/(.*)$ /www/new/$1
AliasMatch ^/foo$ /www/new/$1
<Directory /www/new/>
Options +FollowSymLinks -MultiViews
AllowOverride All
Require all granted
</Directory>
DocumentRoot /www/old
<Directory /www/old/>
Options +FollowSymLinks -MultiViews
AllowOverride All
Require all granted
</Directory>
</VirtualHost>
In both directories I have an .htaccess with:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(.*)$ index.php/$1 [L]
The index.php's show 'old' or 'new' depending on their directory.
What happens to the different routes I test:
When requesting example.com/bar it shows 'old'.
When requesting example.com/foo it redirects too /foo/, and then shows 'new'.
When requesting example.com/foo/bar it shows 'old'.
I don't want the forced redirect of /foo to /foo/. And I want /foo/bar to show 'new'.
I've been following Apache's mod_alias to get the multiple AliasMatch directives to catch anything after /foo but apparently that's not working correctly. Also, I don't read anything there of the trailing slash being added magically.
Anyone knows the magic tricks?
I've been able to fix this by adjusting the Alias to point to the index.php directly:
AliasMatch ^/foo/(.*)$ /www/new/index.php
AliasMatch ^/foo$ /www/new/index.php
Then /bar and anything else goes to the old website, and /foo, /foo/ and /foo/bar goes to the new website.
There's two downsides to this method:
The .htaccess in the new website is skipped completely. But as I mainly use that for letting the index.php pick up every route, that's fine for now.
Frontend resources like css/js go to the old website. Fix that with another simple alias: example.com/frontend/app.css + Alias /frontend /www/new/public_html/frontend.

Apache put : The requested method PUT is not allowed for the URL

I'm trying to allow "put" method on my apache 2.2, but what ever i tried (Limit, LimitExcept ...),
i always got the following error :
405 Method Not Allowed
The requested method PUT is not allowed for the URL
This is my http.conf :
<VirtualHost *:80>
ServerName example.com:80
DocumentRoot "D:/test"
Include "D:/conf/httpd.conf"
<Directory />
Order Allow,Deny
Allow From All
Options Indexes FollowSymLinks MultiViews
<LimitExcept GET PUT POST DELETE>
Order allow,deny
Allow from all
</LimitExcept>
</Directory>
</VirtualHost>
UPDATE :
I readed some related posts like the following :
http://stackoverflow.com/questions/2934554/how-to-enable-and-use-http-put-and-delete-with-apache2-and-php
but i don't have any script php or cgi.
I just want to redirect http call (get, post, put delete ...) to mock files with mod_rewrite like that :
RewriteCond %{REQUEST_URI} ^/maincall/customer
RewriteCond %{REQUEST_METHOD} PUT
RewriteRule /maincall/customer %{DOCUMENT_ROOT}/mockfolders/PUT/data.json
I found this problem when doing server-testing use postman. Actually this problem occur because you typing an error URL. Try fixing the URL that you hit. And it works for my problem
I found a solution :
install perl to create cgi script
define the following rules in the httpd.conf :
AddHandler cgi-script .pl
RewriteCond %{REQUEST_URI} ^/my/url
RewriteCond %{REQUEST_METHOD} PUT
RewriteRule /my/url "C:/Apache/Apache2.2/cgi-bin/myurlput.pl"
and it's worked

Problems with mod_rewrite in Apache (with WAMP)

The Idea: I want to make it that anything that comes after the domain name and ends with .html is treated as a get variable by index.php
Example: www.test.ro/1/2/3.html should actually be www.test.ro/index.php?var=1/2/3.html.
www.test.ro is setup as a virtual host for development, and AllowOverride has value All.
The .htaccess file seems to be processed, but not all the time. If I write a non-recognized rule like 'BizzareRule', the server works without returning a code 500 error.
If I put correct rules between <IfModule mod_rewrite.c></IfModule>, I get an 500 error, even though I have other vhosts using the same conditions and working perfectly.
Here are the contents of my vhost:
<VirtualHost *:80>
ServerAdmin eu#localhost.com
ServerName www.test.ro
ServerAlias test.ro
DocumentRoot D:/Projects/grabsite/test.ro
Options Indexes FollowSymLinks MultiViews
DirectoryIndex index.php index.html
LogLevel warn
ErrorLog D:/Projects/grabsite/test.ro/error.log
CustomLog D:/Projects/grabsite/test.ro/access.log combined
<Directory "D:/Projects/grabsite/test.ro">
Options -Indexes FollowSymLinks
AllowOverride All
Order allow,deny
Allow from all
</Directory>
</VirtualHost>
And test content for .htaccess:
RewriteEngine On
RewriteRule ^/?([^/]*)\.html$ /index.php?seo=$1 [L]
It seems that you enters in infinite loop. Try first to simplify the rule:
RewriteRule ^(.*)\.html$ /index.php?seo=$1 [L]
Or just
RewriteRule ^index.html$ /index.php?seo=$1 [L]
Next, it's best to have RewriteCond -f and RewriteCond -d in order not to execute the rule if you are requesting existing file/folder. In some situations, this can prevent an infinite loop in the rule
The problem is in regex pattern. You said: "The .htaccess file seems to be processed, but not all the time".
If you check your pattern^/?([^/]*)\.html$ you will see that this will work for files in root folder ONLY (e.g. /index.html).
Solution: change pattern to ^(.+)\.html$ and all will be fine -- now it will match ANY URL that ends with .html (e.g. /index.html as well as /hello/pink-kitten.html etc).

mod_rewrite rules and content negotiation

I am relatively new to mod_rewrite, but have a site which I would like to have "pretty urls." Similarly to SO :).
I am attempting to have things like: "http://www.whatever.com/search/test" get rewritten to "http://www.whatever.com/search.php?q=test" and have had some limited success. I believe that content negotiation is getting in my way...
For starters here's my test .htaccess file:
RewriteEngine on
RewriteBase /~user/mysite/
RewriteRule ^search$ search/ [R]
RewriteRule ^search/([^/]*)/?$ search.php?q=$1 [L]
Which unfortunately, does redirect to search.php, but does not pass my param in the q variable. However this does work:
RewriteEngine on
RewriteBase /~user/mysite/
RewriteRule ^search$ search/ [R]
RewriteRule ^search/([^/]*)/?$ s.php?q=$1 [L] # here i've renamed the search.php to s.php to dodge the content negotiation that is happening..
In fact, if I remove the rules all together, I get the same result as with the first version of the file. So my conclusion is that since apache is happily redirecting "foo" to "foo.php" even without any mod_rewrite rules, that it must be the content negotiation that is taking care of it. (This is further verified by the fact if I renamed my foo.php to foo.html, it still will find the file if i just go to "foo").
So, the question is. How do I properly use mod_rewrite with regard to content negotiation? Can I disable it for a particular file? Is there a way to ensure that my mod_rewrite rules happen before the content negotiation happens?
If it is relevant, here is the conf file for the mod_userdir part of my apache conf (this test site is in my user's homedir/public_html):
# Settings for user home directories
<IfDefine USERDIR>
<IfModule userdir_module>
# UserDir: The name of the directory that is appended onto a user's home
# directory if a ~user request is received. Note that you must also set
# the default access control for these directories, as in the example below.
UserDir public_html
# Control access to UserDir directories. The following is an example
# for a site where these directories are restricted to read-only.
<Directory /home/*/public_html>
AllowOverride FileInfo AuthConfig Limit Indexes
Options MultiViews Indexes SymLinksIfOwnerMatch IncludesNoExec
<Limit GET POST OPTIONS>
Order allow,deny
Allow from all
</Limit>
<LimitExcept GET POST OPTIONS>
Order deny,allow
Deny from all
</LimitExcept>
</Directory>
# Suexec isn't really required to run cgi-scripts, but it's a really good
# idea if you have multiple users serving websites...
<IfDefine SUEXEC>
<IfModule suexec_module>
<Directory /home/*/public_html/cgi-bin>
Options ExecCGI
SetHandler cgi-script
</Directory>
</IfModule>
</IfDefine>
</IfModule>
</IfDefine>
Look for this option in your configuration.
Options +Multiviews
It will look for
/foo/test
/foo/
/foo
and redirect them to
/foo.[any file]
based upon if it exists and if it fits the content-type requested.
Change the option to this to disable this.
Options -Multiviews