Adding slashes to the end of directories + more (htaccess) - apache

This is beyond my level and I need some help.
In the htaccess make redirect rules for the following...
if example.com/1stleveldirectory doesn't end in a slash add one.
if example.com/1stleveldirectory/ ends in a slash don't add anything.
if example.com/1stleveldirectory/file is like this add .html.
if example.com/1stleveldirectory/file.html is like this don't add anything.
There are no publicly accessible directories past the first level
Thanks!
EDIT: I should have said I already have this code at the top of the file.
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www.([^.]+.[^.]+)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
Options +FollowSymLinks
RewriteRule ^([^/]*)/([^/]*)\.html$ /index.php?cat=$1&name=$2 [L]
RewriteRule ^([^/]*)/$ /index.php?cat=$1 [L]

Try these rules:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^[^/]+$ %{REQUEST_URI}/ [L,R=301]
RewriteCond $0 !.+\.html$
RewriteRule ^[^/]+/[^/]+$ %{REQUEST_URI}.html [L,R=301]

Related

how to fix htaccess error with multiple rewrite rule - ERR_TOO_MANY_REDIRECTS

why this htaccess has error. it says "to many redirect" please help thanks
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule .* index.php [L]
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
RewriteEngine on
Options +FollowSymLinks
RewriteCond %{THE_REQUEST} ^.*/index\.php
RewriteRule ^(.*)index.php$ http://us.domain.com/ae_en/$1 [R=301,L]
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} ^us\.domain\.com$
RewriteCond %{REQUEST_URI} !^/(ae_en|ae_ar) [NC]
RewriteRule ^(.*)/ae_en/%{REQUEST_URI} [R=301,L]
I cannot try the wrong code, it always appear error
There are several issues with your .htaccess. I would suggest both re-ordering and editing the rules.
This would be a better approach, in my opinion. I'll explain the parts below.
RewriteEngine on
RewriteBase /
Options +FollowSymLinks
# External redirect: Remove www from domain
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
# External redirect: Ensure language code is set
RewriteCond %{HTTP_HOST} ^us\.domain\.com$
RewriteCond %{REQUEST_URI} !^/(ae_en|ae_ar) [NC]
RewriteCond %{REQUEST_URI} !^/index.php$ [NC]
RewriteRule ^(.*)$ /ae_en/$1 [R=301,L]
# Internal redirect: Pass everything non-existent to index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule .* index.php [L]
Structure
First, general options and settings should come at the top and only once, i.e.
RewriteEngine on
RewriteBase /
Options +FollowSymLinks
Second, I like to get the external URL to show up correctly, so I group all external redirects here - denoted with comments in the above code block.
Third, internal "redirects" follow, meaning mapping the (now correct and final) external URL to the correct scripts.
This structure ensures that the internal scripts are only matched on a URL that will not change when re-run against the .htaccess on the next RewriteRule match.
Omitting external redirect for index.php
I guess you added the index.php redirection handling after adding in the internal redirect, which probably has caused index.php to show up in the browser. So, with the above structure in place, I assume it will not show up again.
Error in last RewriteRule
As Mike Rockett pointed out in the comment above, there also was an error in the last RewriteRule.
Avoid redirect loop
To avoid a redirect loop when rewriting to index.php with the last rule, you have to explicitly exclude index.php from language code redirection, i.e.
RewriteCond %{REQUEST_URI} !^/index.php [NC]

this mod_rewrite rule has me for a loop

I want to have /path/short serve up the content held by the file /path/short.html, but if the user types /path/short.html or /path/short/ I want him to be redirected (with 301) to /path/short. This is what I have so far in my .htaccess:
RewriteEngine On
RewriteBase /path
RewriteCond %{REQUEST_URI} !=/path/handle.html
RewriteCond %{REQUEST_URI} !^/path/short(.html|/)?$
RewriteRule ^(.+) /cgi-bin/handle\.cgi?$1 [PT,QSA]
RewriteRule ^path$ /path/short.html
#-------
RewriteRule ^short/$ /path/short [R=301,L]
# RewriteRule ^short.html$ /path/short [R=301,L]
The rules reflect the fact that handle.cgi will receive all other requests (ie, /path/this, /path/, /path/somethingelse, etc.) which is working great. So far, the /path/short/ redirects correctly to /path/short and /path/short is properly showing the content in /path/short.html, however, uncommenting the last line causes a loop. So, how do I get this to work?
You can use this code in /path/.htaccess:
RewriteEngine On
RewriteBase /path/
RewriteCond %{THE_REQUEST} /(short)(/|\.html) [NC]
RewriteRule ^ %1? [R=301,L]
RewriteRule ^(short)/?$ $1.html [L,NC]
A friend pointed me towards another question which helped me arrive at this solution:
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/ads/short(.html|/)?$
RewriteRule ^(.+) /cgi-bin/handle\.cgi?$1 [PT,QSA]
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^short(.html|/)$ /ads/short [R=301]
RewriteRule ^short$ /ads/short.html
which works perfectly.

Mod Rewrite with multiple parameters

I'm trying to setup a rewrite rule so that any text in place of a subdomain will be parameter one and any text after the first forward slash will be parameter two but I'm struggling with regex and am unsure about rewrite terminology.
For example, if someone requested:
joebloggs.mydomain.com
I would like them to see:
mydomain.com/index.php?site=joebloggs
Also, if someone requested:
joebloggs.mydomain.com/contact
I would like them to see:
mydomain.com/index.php?site=joebloggs&page=contact
By "see" I mean see the page, rather than see the URL - it's a CMS-like project so you can probably see where I'm going with it. Also, I've worked out how to remove www. so that's not an issue :)
EDIT
Current .htaccess is:
RewriteEngine On
# Remove trailing slash
RewriteRule ^(.+)/$ $1 [L]
# Remove www
RewriteCond %{HTTP_HOST} ^www.richardmjenkins.com$ [NC]
RewriteRule ^(.*)$ http://richardmjenkins.com/$1 [R=301,L]
# Rewrite for site/page pair
RewriteCond %{HTTP_HOST} ^(.*)\.richardmjenkins\.com$ [NC]
RewriteCond %{REQUEST_URI} !p.php
RewriteRule ^(.+/)?([^/]*)$ p.php?s=%1&p=$2 [QSA,L,NC]
RewriteCond %{HTTP_HOST} ^(.*)\.richardmjenkins\.com$ [NC]
RewriteCond %{REQUEST_URI} !p.php
RewriteRule ^$ p.php?s=%1 [QSA,L,NC]
1. In your host panel : add a subdomain with name : * for enable all subdomains
2. this is your htaccess code :
RewriteEngine On
Options +Followsymlinks
RewriteCond %{HTTP_HOST} ^(.*)\.mydomain\.com$ [NC]
RewriteRule ^$ index.php?site=%1 [QSA,L,NC]
RewriteCond %{HTTP_HOST} ^(.*)\.mydomain\.com$ [NC]
RewriteRule ^([^/]+)/?$ index.php?site=%1&page=$1 [QSA,L,NC]
Enable mod_rewrite and .htaccess through httpd.conf and then put this code in your .htaccess under DOCUMENT_ROOT directory:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^((?!www)[^.]+)\.(mydomain\.com)$ [NC]
RewriteRule ^$ index.php?site=%1 [L,QSA]
RewriteCond %{HTTP_HOST} ^((?!www)[^.]+)\.(mydomain\.com)$ [NC]
RewriteRule ^(.+)$ index.php?site=%1&page=%2 [L,QSA]

Adding a rewrite rule to my existing rules in htaccess

My current htaccess file is this:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteBase /
RewriteRule ^(.*)$ /index.php?$1 [L]
I want to add another rule that redirects www.example.com to http://example.com with a 301 Redirect. I know the rule I need to add is
RewriteCond %{HTTP_HOST} ^www.example.com$ [NC]
RewriteRule ^(.*)$ http://example.com/$1 [R=301,L]
But where do I add this? Do I append this to the end of my current file, or do all Conditions go together, and then all rules together?
Yes you can append , [L] represent the last rule , so first make it your last rule after use redirect [L,R=301]
Use following code in your case
RewriteCond %{HTTP_HOST} !^www.example.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]

Redirect subdomain to subdirectory - trailing slash

I want to redirect [abc].apps.example.com to the following apps.example.com/[abc] but still showing the using the [abc].apps.example.com in the url.
[abc] an be anything eg.
calendar.apps.example.com
books.apps.example.com
etc
With a bit of digging around and tweaking, I have the following setup but doesn't work when an ending slash is missing:
RewriteEngine On
RewriteBase /
# Fix missing trailing slashes.
RewriteCond %{HTTP_HOST} !^www\.apps\.example\.com$ [NC]
RewriteCond %{HTTP_HOST} ^(www\.)?([^\.]+)\.apps\.example\.com$ [NC]
RewriteCond %{DOCUMENT_ROOT}/%2%{REQUEST_URI}/ -d
RewriteRule [^/]$ %{REQUEST_URI}/ [R=301,L]
# Rewrite sub domains.
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{HTTP_HOST} !^www\.apps\.example\.com$ [NC]
RewriteCond %{HTTP_HOST} ^(www\.)?([^\.]+)\.apps\.example\.com$ [NC]
RewriteRule ^.*$ /%2/$0 [QSA,L]
When the following is entered:
calendar.apps.example.com/showcalendar/
it's redirected correctly to: apps.example.com/calendar/showcalendar/
But for when a trailing slash is missing
calendar.apps.example.com/showcalendar
I get a 404 error saying /calendar/calendar/showcalendar/ not found.
Please, if you know what's wrong with the above code or have a better solution do let me know.
Cheers,
Doggy
try the following rewrite rule for adding trailing slashes.
# add trailing slash
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^.*[^/]$ /$0/ [L,R=301]