Forcing www on all URLs without htaccess - django

I have a complex setup wherein am having Django 3 sub-projects kind of thing hosted on single Apache via mod wsgi.
I want to have www prepended on all addresses for one of these as some payment gateways are behaving improper without www URLs. Now, .htaccess's canonical redirect messes up the other 2. Using PREPEND_WWW from common middleware works only for foo.jp and not for foo.jp/bar/* and so on...
Similarly, I also tried to create an index.php, pointed to it from sites-enabled in apache and used header redirection to www.foo.jp for foo.jp...
Please tell me a way for all URLs i.e. anything foo.jp/, foo.jp/bar/ etc. to www.foo.jp/, www.foo.jp/bar/ and so on...
Regards !

I figured this out...
In the apache/sites-enabled/000-default
Basically, the default apache virtual host file which is read before other domain's virtual host files I added this:
<VirtualHost *:80>
ServerName playism.jp
Redirect permanent / http://www.foo.jp/
</VirtualHost>
Now, it is redirecting for everything... without the .htaccess which was messing it up !

Related

301 Redirect domain without www works, but with www does not

Currently I'm working on a request to both redirect the domain A to domain B and redirect a specific page within the domain A to domain B. Here's the expectation in pseudocode:
foo.com =====> https://www.bar.com
foo.com/foo =====> https://www.bar.com/bar
and:
www.foo.com =====> https://www.bar.com
www.foo.com/foo =====> https://www.bar.com/bar
However, only the non-www redirects are working as expected and the www redirects are not functioning at all. Essentially, the first set of redirects above are working and the second set are not.
Here's my Vhost file:
<VirtualHost *:80>
ServerName foo.com
ServerAlias www.foo.com
ErrorLog /etc/httpd/logs/foo.com-error.log
Include /etc/httpd/conf.d/rewrite_rules/foo.com.rewrite
</VirtualHost>
My rewrite rule:
RewriteEngine On
RewriteRule ^/foo$ https://www.bar.com/bar [R=301,L,NC]
RewriteRule (.*) https://www.bar.com [R=301,L,NC]
What's strange is when I test with curl on a test server, it seems as though the URL is being redirected:
301 http://www.foo.com/ https://www.bar.com
I get the feeling I'm missing something in my rewrite rule. Can anyone advise on the problem? Thanks in advance for your help.
Figured out the answer to this issue. As it turns out, the rules in place are all functional and work as intended; however, the SSL certificate on the server was causing a mismatch error.
This mismatch in SSL certificates stopped the redirect functionality. After uploading the necessary certs, everything was working as expected. Hope this answer helps someone in need in the future. Thanks.

Insert a directory at the start of a URL in Apache Config

Apologies if this question has already been asked before. I couldn't find an example that handled my exact situation.
I have an Apache Server and have access to the httpd.conf file.
I have a domain (say www.example.com) and I want to insert a directory (say test) after the domain.
So for example I want www.example.com to be mapped to www.example.com/test and www.example.com/folder to be mapped to www.example.com/test/folder and so on.
I have achieved this using the a RedirectMatch directive like this:-
<VirtualHost *:80>
ServerName www.example.com
RedirectMatch ^/$ test/
</VirtualHost>
However this changes the URL in the browser to include the test folder and I would like to keep this hidden from the end user.
I have tried using a rewrite rule but my lack of regex knowledge has let me down here! This is what I have tried (within the virtual host element):-
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/test/
RewriteRule ^(.*)$ /test/$1
I have tried various combinations with this but nothing seems to work!
Any help would be much appreciated.
Ok, I found the problem here. This was working ok but my browser cache (Firefox) was remembering the old values.
I turned of caching in the browser (by going to about:config and setting browser.cache.disk.enable = FALSE
Then everything started working correctly.
Hopefully this will help others who have the same issue!

using regex for proxypass

I'm trying to set up a proxy to my corporate environment's ActiveMQ admin pages on our standard http proxy server. I have a rule in place that allows me access to the admin landing page:
ProxyPass /foobar hostname:8161/admin
ProxyPassReverse /foobar hostname:8161/admin
However, going to the "queues" page bring me to a different page appended with a unique session ID (admin/queues.jsp;jsessionid=oq37zgvxz4zkwliwdwddyon3), and I would like for this page to be accessible through the same URL as well.
Attempting to add a wildcard (*) at the end of these proxypass rules breaks the redirect. Is there a redirect rule that I need to use in conjunction with this, and how do I get any string to this host to pass through this proxy rule?
There is ProxyPassMatch that is regex based, but ProxyPass does prefix matching (i.e. a ProxyPass for /foo should catch /foo/bar too) so your current rule should work anyway.
It may be that the redirection URL doesn't match the reverse rule for some reason. I have to admit I've never managed to get a ProxyPass to Tomcat to work correctly when the front and back end context paths differ, so my advice would be to deploy the back end app at hostname:8161/foobar instead of /admin if that's an option.

Hosting a wordpress blog parallel with a django app

I would like to host a blog at a subfolder of my domain, which is covered by a django app. I'm most of the way there, but I need some help getting over the finish line, as it were.
Currently, if I go to domain.com, the django app is served correctly. If I go to domain.com/blog/, the blog is served correctly. However, if I go to domain.com/blog (note the missing trailing slash), the urlconf returns a URL not found error.
I've tried a couple of things, including:
Reordering the Alias, Directory, and WSGIScriptAlias statements in my Apache configuration
Having the django urlconf trap the domain.com/blog condition and redirect to domain.com/blog/ (probably unsurprisingly causing an infinite loop of redirects)
What are my next steps?
Here is the relevant part of my Apache conf:
Alias /blog/ /var/www/blog/
<Directory /var/www/blog/>
AllowOverride All
Order deny,allow
Allow from all
</Directory>
I haven't used Apache in years, but try aliasing just /blog instead of /blog/. The problem currently is that Apache is not catching it, so it's being passed to Django. If that doesn't work, you might also try setting up a 301 redirect in your Apache conf to redirect to the slash version, thereby avoiding Django altogether.

Redirect visitor with .htaccess

I've got an e-shop on a virtual server that's been used as a subdirectory for the last few years, but now I'm finally giving the VS it's own domain name. What I really need is visitors to the old URL to be transparently (and 301) redirected to the new URL with everything after /eshop/ maintained and apended to the new host.
I.e. http://www.example.com/eshop/page.php -> http://www.newdomain.com/page.php
Any help would be greatly appreciated.
This should work with Apache:
RewriteEngine On
RewriteBase /
RewriteRule ^/eshop(/.*)? http://www.newdomain.com$1 [R=permanent,L]
This redirects http://www.example.com/eshop/whatever to http://www.newdomain.com/whatever and also redirects http://www.example.com/eshop to http://www.newdomain.com
You did not specify which web server you were using, but I assume its either apache or lighttpd.
In apache, you can use the Redirect keyword, e.g.
Redirect 301 / http://www.newdomain.com/
I haven't tried this, but see e.g. here:
http://www.yolinux.com/TUTORIALS/ApacheRedirect.html#APACHE
It seems to work with .htaccess files as well.
In lighttpd, there is mod_redirect (and I did try this :) ):
http://redmine.lighttpd.net/wiki/1/Docs:ModRedirect
EDIT:
Redirect 301 /eshop/ http://www.newdomain.com