RewriteURL with apache2 #username to /user/username - mod-rewrite

This is fairly simple but I am having a hard time figuring out what to do I searched in the apache2 rewriteurl documentation but didnt find anything helpful.
you go to url.com/#myusername and apache points you to url.com/username/#myusername and keeps the URL on the browser intact with [L,NE,P]
RewriteURL #username /user/username
please note I only want to redirect what starts with # sign and take it as a username

First activate mod_rewrite,
sudo a2enmod rewrite
Then restart apache2,
sudo systemctl restart apache2
create the .htaccess file in the web root. This must be the document root you provided in the apache config.
RewriteRule ^([A-Za-z0-9]+) user/$1 [QSA]
UPDATE
If you want the match start with # symbol, use the regex as below,
RewriteRule ^(#[A-Za-z0-9]+) user/$1 [QSA]

Related

Moodle 3.5 with Reverse proxy

I'm having troubles setting up a moodle instance behind an apache proxy.
Here's my apache front-end that proxies to the running server.
<VirtualHost *:80>
ServerName public.domain.com
ProxyRequests Off
ProxyPreserveHost On
ProxyPass / http://10.10.10.10:81/moodle/
ProxyPassReverse / http://10.10.10.10:81/moodle/
</VirtualHost>
AND.
$CFG->wwwroot = 'http://public.domain.com';
I install without problems, but when finished the installation I try in browser:
http://public.domain.com
This redirect to: http://public.domain.com/moodle/index.php?sessionstarted=1&lang=en...
Does anyone know what might be happening?
The best way to fix this issue is to move the moodle installation on the internal host to the root of web server.
Move your moodle in 10.10.10.10 to be at / and not at /moodle
Note, that if you will use SSL on the external apache (it's suggested) you should also add to your config this line:
$CFG->sslproxy = true;
Finally I have been able to fix the problem, I am writing this answer with a bigger level of detail so other people having this problem can follow my answer.
First we need to edit the apache2 config for our site:
In general the apache2 configuration for your specified site can be found at /etc/apache2/sites-enabled. Depending if you are using http or https you need to edit the right configuration file. Default name for http is 000-default.conf and for https 000-default-ssl.conf
Add the following lines between the <VirtualHost *:80>....</VirtualHost> sections.
# MOODLE
ProxyRequests Off
ProxyPreserveHost On
ProxyPass "/" "http://10.10.10.10:81/moodle/"
ProxyPassReverse "/" "http://10.10.10.10:81/moodle/"
Then we need to restart our apache2 webserver, this can be done with the command service apache2 restart.
Now we also need to edit a few things in our moodle config.php file. This file can be found at /var/www/html/moodle on the server with the IP (in this case) 10.10.10.10 if you used the default install location from the moodle guides.
In the config.php file we append the following lines under the default $CFG declarations: Please make sure to change all values according to your server setup.
$CFG->wwwroot = 'http://public.domain.com';
$CFG->dirroot = '/var/www/html/moodle';
$CFG->reverseproxy = true;
//$CFG->sslproxy = true; //UNCOMMENT this line if you are using SSL
Attention: If you are not using the root directory at your public webserver, then make sure that you do not use the same directory as moodle is using on the subserver. For example http://public.domain.com/moodle will fail if moodle is installed on the subserver at /var/html/moodle since both directories are equal and the proxy loops for some reason. My easy fix for this problem was do just move the moodle installation to /var/html/moodley including all required changes in the config.php. This fixes every problem I had.

URL issue in XAMPP Windows

I am facing a problem with a project recently. The site is running locally fine. Like if I browse the site its showing me the page also if I do some thing like "localsite.dev/students" it is also showing the page. But if I type "localsite.dev/students:auth", I am getting "Access forbidden!". I am using XAMPP in Windows 8. Here is my configuration:
Apache Version Apache/2.4.10 (Win32) OpenSSL/1.0.1t PHP/5.6.24
PHP Version 5.6.24
Is it a problem with the .htaccess file or any Apache settings problem. Please let me know. The .htaccess file that I am using is like following:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php [L]
Please help me. I have already spent a lot of time with it. Thanks in advance for any kind of help.
Apache runs as the "nobody" user by default, it might have permission to browse your [OSX/Win] user directory or some of its subdirectories.So, you will receive "access forbidden" error when you try and view your development site. Another scenario will be that you might find that you are able to view your dev site, but PHP throws errors when you attempt to write files or make directories on the filesystem.
So, to fix this you can configure Apache to run as your [OSX/Win] user. Open httpd.conf and look for following lines:
# User/Group: The name (or #number) of the user/group to run httpd as.
# It is usually good practice to create a dedicated user and group for
# running httpd, as with most system services.
#
User nobody
Group nogroup
Change this to:
User Username01
Restart Apache and you are now good to go to navigate your site without any issues, including manipulating files and folders using PHP.
Refer the link for reference:
Virtual Hosts in xampp
Hope It helps you.

Configuring CakePHP .htaccess for Cpanel ~temp/ URL

I'm trying to set up CakePHP 2.x in Cpanel (Hostmonster) while the domain of the site is still pointing to the older server. In order to pull up the site without the domain (for demo), I have to use http://[hosting-IP-address]/~mysite.
This is my default .htaccess file located in the root (the one before app/ folder):
RewriteEngine on
RewriteRule ^$ app/webroot/ [L]
RewriteRule (.*) app/webroot/$1 [L]
If I leave it as it is, I get 404 error. When I remove the .htaccess file or leave it blank, the site content loads, but without CSS. The site files are located in /home2/mysite/public_html/ folder. I tried adding RewriteBase with different combinations (~mysite, mysite, mysite/public_html), but no avail.
I have tried with rewrite base setting then i get 500 internal server error. Please suggest
Instead of changing your site .htaccess code. I will suggest you update your local pc host file to point your site to new server, so that you can check your all URL.
Open your hosts file which is located at : c:\Windows\System32\Drivers\etc\hosts and add the following code with your new hosting IP address
hosting-IP-address www.domain.com
hosting-IP-address domain.com

Why does fatfree need a RewriteBase on Mac but not Ubuntu?

I've used FatFree quite a lot recently on Ubuntu, and have had no problems putting it into subdirectories. However, I recently tried to do this in a user website on Mac (10.6.8) and ran into the problem of needing a RewriteBase for the routing to work.
I was wondering if anyone has any idea why the default Apache setup on Ubuntu seems to allow the routing to work without a RewriteBase in the .htaccess, but the setup on Mac makes it necessary.
The Ubuntu setup uses a bunch of VirtualHosts, whereas the Mac config just has a server config, mod_userdir enabled and a directive for each user's Sites directory.
The mod_rewrite documentation says that the RewriteBase directive is required unless either of the following conditions are true:
The original request, and the substitution, are underneath the DocumentRoot (as opposed to reachable by other means, such as Alias).
The filesystem path to the directory containing the RewriteRule, suffixed by the relative substitution is also valid as a URL path on the server (this is rare).
The first condition is probably met with your Ubuntu setup but not with your Mac setup, since you are using mod_userdir. If you move your files from ~/Sites to the DocumentRoot (default on Mac is /Library/Webserver/Documents), chances are that you won't need the RewriteBase anymore.
I'm not sure, but i can tell you that most apache setups have RewriteBase / as default value, which makes it superfluous to write it in your .htaccess file once more, if your F3 is on webroot. Maybe this portion is missing in a MAMP setup on mac.
Running F3 in a subdir of the virtualhost or webroot usually always requires a valid definition of RewriteBase.
Additional information about this can be found here:
https://github.com/bcosca/fatfree/wiki/FAQ#can-i-run-f3-in-a-sub-directory
Let me just explain how you should analyze the difference between the Mac and Ubuntu configurations so you can understand why RewriteBase is required by one but not the other. Add this to your Apache config:
RewriteLog /var/log/apache2/error.log
RewriteLogLevel 9

mod_rewrite issues with something that used to work

I have a virtual private server that was borked over the weekend, and has been upgraded. Sometime between now and then, some changes have been made that have impacted a mod_rewrite call, but I'm not sure which.
In any event, I have a folder with a PHP script that emulates a CAS server. The system used to work fine and dandy until today. Now, instead of the mod_rewrite directives providing commands via rewrite to the index.php file, Apache goes '404' and sends me up a directory to Wordpress's 404 handler.
This is the htaccess code that used to work:
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
RewriteEngine On
RewriteRule ^([A-Za-z0-9-]+)/?$ index.php?mode=$1 [QSA, L]
The URL variables appeared in CAS-appropriate commands (like cas/login, cas/logout), but are now generating 404s.
There may have been Apache tweaking; do I need to add any commands to the htaccess (like Options Indexes) or something to make Apache listen to the rewrite commands?
Oddly, the force-https command seems to work.

Resources