I'm new to mod_rewrite, but I have successfully set up an Apache 2.4 Reverse Proxy (doesn't use <directory>). However, now I need to redirect two literal paths (app1 and app2 to app1/ and app2/):
https://external.com/app1 to https://internal.com/app1/ (with the slash)
https://external.com/app2 to https://internal.com/app2/ (with the slash)
where app1/ and app2/ are
ProxyPass "/app1/" "https://internal.com/app1/"
ProxyPassReverse "/app1/" "https://internal.com/app1/"
ProxyPass "/app2/" "https://internal.com/app2/"
ProxyPassReverse "/app2/" "https://internal.com/app2/"
I've tried variations on these, but they cause loops or otherwise don't work.
RewriteEngine On
RewriteRule ^/(app1)$ $1/ [PT]
RewriteRule ^/(app2)$ $1/ [PT]
Thoughts? Again, I don't know mod_rewrite well.
I got this working with redirect instead of mod_rewrite:
Redirect /app1 /app1/
Redirect /app2 /app2/
Related
I got a server running on http. With reverse proxy I was able to wrap it into https and server it as https.
The http server runs on port 9000 and expects the GET parameter action.
So with this config, I can wrap my http in https:
SSLEngine On
Include /etc/letsencrypt/options-ssl-apache.conf
SSLCertificateFile /etc/letsencrypt/live/example.com/fullchain.pem
SSLCertificateKeyFile /etc/letsencrypt/live/example.com/privkey.pem
SSLCertificateChainFile /etc/letsencrypt/live/example.com/chain.pem
SSLProxyEngine On
ProxyPass /snapshot/ http://localhost:9000/
ProxyPassReverse /snapshot/ http://localhost:9000/
However I still want to redirect from /snapshot/ to a query parameter after a slash:
https:// example.com/snapshot/ -> https:// example.com:9000/?action=snapshot
I can't get my head around this redirection. I tried some RewriteRule but only could get it to get redirected to /snapshot?action=snapshot.
Please be aware that the parameter should be after the slash and that the URL shouldn't display the action parameter.
How do I manage this?
Using the RewriteRule instead of the ReverseProxy did the trick. Replacing the SSLProxyEngine On lines with these lines works for me:
RewriteEngine On
RewriteRule snapshot/ http://localhost:9000/?action=snapshot [P]
RewriteRule stream/ http://localhost:9000/?action=stream [P]
Today I went from my Windows server to Ubuntu. I transferred my Laravel project that I had on IIS to apache2 and installed mod rewrite. However, I can't see the pages. This is the error I get: View [pages.maintenance] not found.
This is what I added to the 000-default.config configuration file:
<Directory /var/www/html>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</ Directory>
This is my htaccess (found in /var/www/html/public, (my DirectoryRoot):
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews -Indexes
</ IfModule>
RewriteEngine On
# Handle Authorization Header
RewriteCond% {HTTP: Authorization}.
RewriteRule. * - [E = HTTP_AUTHORIZATION:% {HTTP: Authorization}]
# Redirect Trailing Slashes If Not A Folder ...
RewriteCond% {REQUEST_FILENAME}! -D
RewriteCond% {REQUEST_URI} (. +) / $
RewriteRule ^% 1 [L, R = 301]
# Handle Front Controller ...
RewriteCond% {REQUEST_FILENAME}! -D
RewriteCond% {REQUEST_FILENAME}! -F
RewriteRule ^ index.php [L]
</ IfModule>
How come I get this? I checked the rewritten mod change and it's okay.
I am using apache2.4 on ubuntu 14.04. And I have enabled mod_proxy_html.But my URL are not getting replaced by a new one. Same thing I did in apache2.2 and it was working perfectly. This is my Virtual host file.
<VirtualHost *:80>
ServerAdmin webmaster#example.com
ServerName www.example.com
ServerAlias example.com
DocumentRoot /var/www/html/www.example.com
LogLevel error
CustomLog /var/log/apache2/www.example.com_access.log combined
ErrorLog /var/log/apache2/www.example.com_error.log
#Proxy and cookies settings
ProxyPreserveHost On
ProxyPassReverse / http://www.example.com/example/control/
ProxyPassReverse / https://www.example.com/example/control/
ProxyPassReverse / /example/control/
ProxyPassReverseCookiePath /example /
<Proxy balancer://cluster>
BalancerMember ajp://10.14.78.45:8009 route=node01 keepalive=On loadfactor=1 ping=10 ttl=600
ProxySet timeout=60 stickysession=JSESSIONID nofailover=On
</Proxy>
RewriteEngine On
#redirect non www domain to www domain
RewriteCond %{HTTP_HOST} ^example.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com$1 [R=301,L]
#Website
RewriteRule ^/;(.*)$ balancer://cluster/example/control/main;$1 [P,L]
RewriteRule ^/$ balancer://cluster/example/control/main [P,L]
SetOutputFilter INFLATE;proxy-html;DEFLATE
ProxyHTMLExtended Off
ProxyHTMLInterp On
ProxyHTMLDoctype XHTML Legacy
LogLevel debug
#Rewrite home page link
ProxyHTMLURLMap ^(.*)/example/control/main;(.*)$ $1/;$2 [R]
ProxyHTMLURLMap ^(.*)/example/control/main$ $1/ [R]
I guess ProxyHTMLURLMap is not working here. Please let me know if something is not configured properly.
Thanks
After hours of research I got simple solution with little luck.
proxy_html.conf file was missing. I have copied this file from apache2.2 to /etc/apache/mods-available and made link in mods-enabled pointing to this file.
vi /etc/apache2/mods-available
added these lines:
# Here's the declaration for W3C HTML 4.01 and XHTML 1.0
ProxyHTMLLinks a href
ProxyHTMLLinks area href
ProxyHTMLLinks link href
ProxyHTMLLinks img src longdesc usemap
ProxyHTMLLinks object classid codebase data usemap
ProxyHTMLLinks q cite
ProxyHTMLLinks blockquote cite
ProxyHTMLLinks ins cite
ProxyHTMLLinks del cite
ProxyHTMLLinks form action
ProxyHTMLLinks input src usemap
ProxyHTMLLinks head profile
ProxyHTMLLinks base href
ProxyHTMLLinks script src for
# To support scripting events (with ProxyHTMLExtended On),
# you'll need to declare them too.
ProxyHTMLEvents onclick ondblclick onmousedown onmouseup \
onmouseover onmousemove onmouseout onkeypress \
onkeydown onkeyup onfocus onblur onload \
onunload onsubmit onreset onselect onchange
creating link in mods-enabled
ln -s /etc/apache2/mods-available/proxy_html.conf /etc/apache2/mods-enabled/
After that it worked perfectly
I have a site where I am trying to rewrite search/ks3/7/example-topic to search.php?ks=ks3&year=7&topic=example-topic
I am using apache mod_rewrite and my .htaccess looks like this:
RewriteEngine On
RewriteBase /
RewriteRule ^search/(a-zA-Z0-9+)/(a-zA-Z0-9+)/(a-zA-Z0-9-+) search.php?ks=$1&year=$2&topic=$3 [R=301,L]
However, when I browse to search/ks3/7/example my php $_GET['ks'], $_GET['topic'] and $_GET['year'] contain no data.
My apache error log shows errors in the PHP "undefined index ks" etc.
My rewrite log (which is set to a verbosity of 9) shows:
192.168.0.171 - - [08/Jan/2012:13:11:46 +0000] [192.168.0.10/sid#7ff3cc9b7460][rid#7ff3c65f90a0/initial] (3) [perdir /data/shared/chemistry/] add path info postfix: /data/shared/chemistry/search.php -> /data/shared/chemistry/search.php/ks3/7/example-topic
192.168.0.171 - - [08/Jan/2012:13:11:46 +0000] [192.168.0.10/sid#7ff3cc9b7460][rid#7ff3c65f90a0/initial] (3) [perdir /data/shared/chemistry/] strip per-dir prefix: /data/shared/chemistry/search.php/ks3/7/example-topic -> search.php/ks3/7/example-topic
192.168.0.171 - - [08/Jan/2012:13:11:46 +0000] [192.168.0.10/sid#7ff3cc9b7460][rid#7ff3c65f90a0/initial] (3) [perdir /data/shared/chemistry/] applying pattern '^search/(a-zA-Z0-9+)/(a-zA-Z0-9+)/(a-zA-Z0-9-+)$' to uri 'search.php/ks3/7/example-topic'
192.168.0.171 - - [08/Jan/2012:13:11:46 +0000] [192.168.0.10/sid#7ff3cc9b7460][rid#7ff3c65f90a0/initial] (1) [perdir /data/shared/chemistry/] pass through /data/shared/chemistry/search.php
192.168.0.171 - - [08/Jan/2012:13:11:46 +0000] [192.168.0.10/sid#7ff3cc9b7460][rid#7ff3c65f00a0/subreq] (1) [perdir /data/shared/chemistry/] pass through /data/shared/chemistry/ks3
and my apache virtual hosts file looks like this
<VirtualHost *:80>
ServerAdmin powerj96#hotmail.co.uk
DocumentRoot /data/shared/chemistry
<Directory /data/shared/chemistry/>
Options Indexes FollowSymLinks MultiViews
AllowOverride All
</Directory>
<Directory />
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Order allow,deny
allow from all
</Directory>
</VirtualHost>
Mod_Rewrite is definitely working. I have tried the rule:
RewriteRule ^google http://www.google.com [R=301,L]
and this works.
I also run 4 other virtual hosts on the server, 3 of which are just websites and 1 is transmission's web interface.
I am running Ubuntu Server 11.11
EDIT:
I think the problem may be to do with running reverse proxy alongside this. for example, if reverse proxy is changing 192.168.0.10:81 (address of my server and port 81 vhost) to transmission/web then the fact that transmission is running at 192.168.0.10/transmission/web, which is both a vhost and a subdirectory of the site which is running at 192.168.0.10/ (the one in question on this post), may be causing conflicts in the rewrite module. Do you have any ideas of a solution to this if you think it may be the problem.
EDIT:
This is the complete log for everything that happens when I click the link search/ks3/7/example-topic
(1) [perdir /data/shared/chemistry/] pass through /data/shared/chemistry/search.php
(3) [perdir /data/shared/chemistry/] add path info postfix: /data/shared/chemistry/search.php -> /data/shared/chemistry/search.php/ks3/7/example-topic
(3) [perdir /data/shared/chemistry/] strip per-dir prefix: /data/shared/chemistry/search.php/ks3/7/example-topic -> search.php/ks3/7/example-topic
(3) [perdir /data/shared/chemistry/] applying pattern '^search/([^/]+)/([^/]+)/([^/]+)' to uri 'search.php/ks3/7/example-topic'
(1) [perdir /data/shared/chemistry/] pass through /data/shared/chemistry/search.php
(1) [perdir /data/shared/chemistry/] pass through /data/shared/chemistry/ks3
EDIT:
I have realised the source of the problem after looking at that log file. For some reason apache is rewriting search/ks3/7/example-topic to search.php/ks3/7/example-topic before it applies my rewrite, therefore search/ks3/7/example-topic doesn't satisfy the
^search/([^/]+)/([^/]+)/([^/]+)
rule. I have changed the rule to
^search.php/([^/]+)/([^/]+)/([^/]+)
and this works.
Do you have any idea of how to stop apache rewriting search/ks3/7/example-topic to search.php/ks3/7/example-topic ?
It is working now, after removing the QSA, NC and R=301 options.
Your rewritelog doesn't tell much. Remove IP information at the beginning (who cares?). And add the beginning of rewrite (= tell us what is the uri that will be rewritten): take a look at the first line of the following log.
Here's an example of what should be more helpful:
(2) init rewrite engine with requested uri /robots.txt
(3) applying pattern '(.*)setup.php' to uri '/robots.txt'
(3) applying pattern '(.*)admin(.*)' to uri '/robots.txt'
(3) applying pattern '(.*)' to uri '/robots.txt'
(4) RewriteCond: input='www.papdevis.com' pattern='!(papdevis)' [NC] => not-matched
(3) applying pattern '(.*)' to uri '/robots.txt'
(4) RewriteCond: input='www.papdevis.com' pattern='(.*)\.(com|net|org|eu)' [NC] => matched
(2) rewrite '/robots.txt' -> 'http://www.papdevis.fr/robots.txt'
(2) explicitly forcing redirect with http://www.papdevis.fr/robots.txt
(1) escaping http://www.papdevis.fr/robots.txt for redirect
(1) redirect to http://www.papdevis.fr/robots.txt [REDIRECT/301]
(2) init rewrite engine with requested uri /
(3) applying pattern '(.*)setup.php' to uri '/'
(3) applying pattern '(.*)admin(.*)' to uri '/'
(3) applying pattern '(.*)' to uri '/'
(4) RewriteCond: input='www.papdevis.com' pattern='!(papdevis)' [NC] => not-matched
(3) applying pattern '(.*)' to uri '/'
(4) RewriteCond: input='www.papdevis.com' pattern='(.*)\.(com|net|org|eu)' [NC] => matched
(2) rewrite '/' -> 'http://www.papdevis.fr/'
(2) explicitly forcing redirect with http://www.papdevis.fr/
(1) escaping http://www.papdevis.fr/ for redirect
(1) redirect to http://www.papdevis.fr/ [REDIRECT/301]
Rewrite from:
search/ks3/7/example-topic
to
search.php?ks=ks3&year=7&topic=example-topic
And your .htaccess file looks like this:
RewriteEngine On
RewriteBase /
RewriteRule ^search/(a-zA-Z0-9+)/(a-zA-Z0-9+)/(a-zA-Z0-9-+) \
search.php?ks=$1&year=$2&topic=$3 [R=301,L]
It will never work... try this:
RewriteEngine On
RewriteBase /
RewriteRule ^search/([a-zA-Z0-9]+)/([a-zA-Z0-9]+)/([a-zA-Z0-9-]+) \
search.php?ks=$1&year=$2&topic=$3 [QSA,NC,R=301,L]
... or maybe how about taking everything but the slash? This may look like that:
RewriteEngine On
RewriteBase /
RewriteRule ^search/([^/]+)/([^/]+)/([^/]+) \
search.php?ks=$1&year=$2&topic=$3 [QSA,NC,R=301,L]
Please tell me if this works.
Two hints:
If you're not in a hosted environment (= if it's your own server and you can modify the virtual hosts, not only the .htaccess files), try to use the RewriteLog directive: it helps you to track down such problems:
# Trace:
# (!) file gets big quickly, remove in prod environments:
RewriteLog "/web/logs/mywebsite.rewrite.log"
RewriteLogLevel 9
RewriteEngine On
My favorite tool to check for regexp:
http://www.quanetic.com/Regex (don't forget to choose ereg(POSIX) instead of preg(PCRE)!)
The most succinct way of summarizing the problem at hand:
Development is over, and everything was run against frontend_dev.php during development and testing
This means that all URLs were: server.com/frontend_dev.php/module/action/parm
Moving to production means switching environments, and thusly using index.php instead
server.com/index.php/module/action/parm
Part of moving to production is using mod_rewrite under Apache2 to make the “index.php” part of the URL vanish, but still be functioning
server.com/module/action/parm is still routed against index.php
The URLs are indeed appearing w/o the index.php part, but symfony routing is now complaining:
ie, server.com/goals which routes to goals/index
-- perfectly fine using frontend_dev.php or index.php as an explicit controller
server.com/index.php/goals
-- using no explicit controller (via rewrite):
[Tue Dec 14 12:59:51 2010] [error] [client 75.16.181.113] Empty module and/or action after parsing the URL "/goals/" (/)
I have verified the rewrite is indeed routing to index.php by changing the rewrite to something that doesn’t exist:
[Tue Dec 14 13:05:43 2010] [error] [client 75.16.181.113] script '/opt/www/projects/adam/web/index2.php' not found or unable to stat
I have tried rerouting to frontend_dev.php, but only am provided with more debug information from symfony, none of which is helpful:
404 | Not Found | sfError404Exception Empty module and/or action after parsing the URL "/goals/" (/).
stack trace
1. at () in SF_SYMFONY_LIB_DIR/controller/sfFrontWebController.class.php line 44 ...
2. at sfFrontWebController->dispatch() in SF_SYMFONY_LIB_DIR/util/sfContext.class.php line 170 ...
3. at sfContext->dispatch() in SF_ROOT_DIR/web/frontend_dev.php line 13 ...
I have tried the using the RewriteBase option in .htaccess, but that does not help any, nor changing the true/false in the configuration line of the controllers
I hope this provides enough to understand why we’re confused, and able to direct us to a resolution.
Following is the current .htaccess and index/frontend configuration lines
Index.php:
$configuration = ProjectConfiguration::getApplicationConfiguration('frontend', 'prod', false);
Frontend_dev.php:
$configuration = ProjectConfiguration::getApplicationConfiguration('frontend', 'dev', true);
.htaccess:
RewriteEngine On
# uncomment the following line, if you are having trouble
# getting no_script_name to work
#RewriteBase /
# we skip all files with .something
#RewriteCond %{REQUEST_URI} ..+$
#RewriteCond %{REQUEST_URI} !.html$
#RewriteRule .* - [L]
# we check if the .html version is here (caching)
RewriteRule ^$ index.html [QSA]
RewriteRule ^([^.]+)$ $1.html [QSA]
RewriteCond %{REQUEST_FILENAME} !-f
# no, so we redirect to our front web controller
RewriteRule ^(.*)$ index.php [QSA,L]
I had similar issue and setting 'AllowOverride' to ALL for Symfony's WEB folder in Virtual Host's config sorted out this problem.
Welcome to Stack Overflow.
Maybe you're confusing the "index" route with "index.php"?
These URLs should theoretically all work.
server.com/frontend_dev.php/goals/index
server.com/index.php/goals/index
server.com/goals/index
server.com/goals
I can't remember if the trailing slash, like server.com/goals/, works or not. There's a gotcha there.