(.htaccess) how to mod_rewrite a tilde user site to subfolder within same site - apache

I have been working this issue for several nights with no luck. I'm trying to Rewrite web requests using (.htaccess)
from: cobweb.seas.gwu.edu/~mpnl
to: cobweb.seas.gwu.edu/~mpnl/joomla
My latest (.htaccess) file is below:
# turn on Rewrite
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www.)?cobweb.seas.gwu.edu/~mpnl$
RewriteCond %{REQUEST_URI} !^/joomla/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /joomla/$1 [L]
RewriteRule ^(/)?$ joomla/index.php [L]

You can't match against the URI path with the %{HTTP_HOST} var, only the host. You'll need to either include the ~mpnl part as a rewrite base or as part of the request URI:
RewriteEngine on
# remove the "/~mpnl" from the end of this line, add a [NC]
RewriteCond %{HTTP_HOST} ^(www.)?cobweb.seas.gwu.edu$ [NC]
# add a "/~mpnl" to here
RewriteCond %{REQUEST_URI} !^/~mpnl/joomla/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# remove the leading slash before "joomla"
RewriteRule ^(.*)$ joomla/$1 [L]
# add a condition to this:
RewriteCond %{REQUEST_URI} !^/~mpnl/joomla/
RewriteRule ^(/)?$ joomla/index.php [L]

To rewrite requests with a UserDir in the URI, you have to set a RewriteBase:
RewriteBase /~mpnl/

Related

Adding directory to url with htaccess & rewrite

I have a site based on Modx CMS and I need to create a 301 redirect for the main folder & old links after the multilanguage functionality has been enabled.
What I need to achieve is the default fallback to english eg. make sure that:
example.com/products -> example.com/en/products
example.com/about - example.com/en/about
etc.
I also need to make sure that if there already is a language selection (for example de) in the url, I don't add en to url. (so no example.com/en/de/products)
I am having trouble adding the /en/ to url and I am ending up with infite /en/en/en loops on the URL
To add the /en/ to the url I tried the following.
RewriteCond %{REQUEST_URI} !^/de$
RewriteCond %{HTTP_HOST} .*example.com [NC]
RewriteRule ^https://example.com/en/%{REQUEST_URI} [L,R=301]
It results to a endless /en/en/en loop in the url.
The whole htaccess is as follow:
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule ^.*$ https://%{SERVER_NAME}%{REQUEST_URI} [R,L]
RewriteBase /
RewriteCond %{REQUEST_URI} !^/de$
RewriteCond %{HTTP_HOST} .*example.com [NC]
RewriteRule ^https://example.com/en/%{REQUEST_URI} [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]
Try the code below. The rules are chained (the default is AND). So, if your URL doesn't start with /en, /de, it's not the index (/), and it's not a file name or a directory, do a 301 redirect to the English version:
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/en
RewriteCond %{REQUEST_URI} !^/de
RewriteCond %{REQUEST_URI} !^/$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ https://%{HTTP_HOST}/en%{REQUEST_URI} [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]

Issue redirecting query string to path

I want to redirect a get query parameter to a path and maintain the ability to use the GET parameter in the code.
I struggle to find examples excluding the file extension so perhaps this is what is throwing me.
I have tried the below code but not getting expected results from it?
RewriteCond %{REQUEST_URI} ^/spares/$
RewriteCond %{QUERY_STRING} pid=([0-9]*)$
RewriteRule ^(.*)$ /spares/%1 [R=302,L]
For example:
mysite.com/spares/?pid=x
to
mysite.com/spares/x
EDIT:
Copy of .htaccess in /dev/ subdirectory.
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /dev/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /dev/index.php [L]
</IfModule>
RewriteEngine On
# To externally redirect with pid
RewriteCond %{THE_REQUEST} \s+spares/\?pid=(.*)\s [NC]
RewriteRule ^ /spares/%1 [R=301,L,NE]
# To internally rewrite to ?pid=
RewriteCond %{QUERY_STRING} pid=(.+)$
RewriteRule ^dev/spares/$ /dev/spares/?pid=%1 [NC,L]
# END WordPress
Try with:
RewriteEngine On
# To externally redirect with pid
RewriteCond %{THE_REQUEST} \s/+spares/\?pid=(.*)\s [NC]
RewriteRule ^ /spares/%1 [R=301,L,NE]
# To internally rewrite to ?pid=
RewriteCond %{QUERY_STRING} pid=(.+)$
RewriteRule ^spares/$ /spares/?pid=%1 [NC,L]

Apache Rewrite if file not found to another file?

I am trying to rewrite my urls to another file if they do not already exist. I have an .htaccess file in the root of my site that looks like this.
RewriteEngine on
# Add slash to end of request
RewriteCond %{REQUEST_URI} !(/$|\.)
RewriteRule (.*) %{REQUEST_URI}/ [R=301,L]
# API redirect
RewriteCond %{REQUEST_URI} ^/api
RewriteRule . api/v1.php [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{HTTP_HOST} ^ secondary\.
RewriteRule . sites/secondary [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule . sites/main.php [L]
My intention is to serve two sites from the same folder and use the rewrites to point to the appropriate routers. The api redirect works great. However, the main and secondary redirect do not work unless I specify a route other than example.com (example.com/test/ correctly rewrites).
Any insight is greatly appreciated.
Use:
RewriteEngine on
# Add slash to end of request
RewriteCond %{REQUEST_URI} !(/$|\.)
RewriteRule ^ %{REQUEST_URI}/ [R=301,L]
# API redirect
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^api api/v1.php [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{HTTP_HOST} ^ secondary\.
RewriteRule ^ sites/secondary [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ sites/main.php [L]
Because . is at least equal to one character.

htaccess redirect to subfolder with a trailing slash

I successfully managed to redirect all requests to a subfolder of my web server.
This is the code I'm using:
/.htaccess
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_URI} !^/public/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /public/$1 [L]
/public/.htaccess
# redirect all urls that doesn’t have a
# trailing slash to urls with a trailing slash
# (this is my try)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ $1 [NC]
It works perfectly, so the following redirects work:
example.com -> example.com/public/
example.com/foo/ -> example.com/public/foo/
example.com/foo/about.html -> example.com/public/foo/about.html
By the way, the following redirect has a problem:
example.com/foo -> example.com/public/foo
Although it redirects correctly, the actual URL in the address bar is:
example.com/public/foo/
So my question is:
How can I have a clean URL when redirecting from a URL without a forward slash?
Hope it makes sense.
Place this code in root .htaccess:
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)/$ $1 [R=301,L]
RewriteCond %{REQUEST_URI} !^/public/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /public/$1/ [L]
Then just remove /public/.htaccess since that is not needed.

Rewrite to add a trailing slash, but independent of domain?

I'm using Apache and mod_rewrite to rewrite URLs for my web app. You can see it here:
RewriteEngine On
RewriteBase /
# www. to non-www.
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
# Redirect non-existant files so there's a trailing slash
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ $1/ [R=301,L]
# Send the URL to index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php [L,QSA]
All working fine, but the problem is the trailing slash rewrite. It works when I'm at the root of the domain, but in my staging environment I'm running this app within a subdirectory. I'm having to modify the RewriteBase directive to include the subdirectory or the rewrite fails.
I'm looking for a solution that will add a trailing slash to the URL - regardless of whether the app is running on the root of the server, without having to change the RewriteBase. Thanks in advance.
After poking around and some help by #MortBM, looks like the below works well:
RewriteEngine On
# www. to non-www.
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
# Add a trailing slash to any non-existent files
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ %{REQUEST_URI}/ [R=301,L]
# Send the URI to index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php [QSA,L]
Summary: removing the RewriteBase, and using %{REQUEST_URI} within the redirect did it :)