htaccess modrewrite to rename directory for SEF - .htaccess

I'm wanting to change directory /rooms to show the name /gear for SEF without having to go through and change the directory name in our script. Note: the directory "gear" does not exist.
For example I want to change:
site/private/rooms/new
to say
site/private/gear/new
My current htaccess says:
RewriteEngine ON
RewriteCond %{THE_REQUEST} \ /(.*)/rooms([^\ ]+)? [NC]
RewriteRule ^ /%1/gear%2 [L,R]
RewriteRule ^(.*)/gear(/.*)?$ /$1/rooms$2 [NC,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php?/$0 [PT,L]
And it takes me exactly to the directory that I want site.com/gear/new
However, I get a 404 error. I believe because there is no content there? I even tried duplicating folder /rooms and renaming it /gear and I still get a 404.
When the code is:
RewriteEngine ON
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php?/$0 [PT,L]
#RewriteRule ^(.*)$ /index.php?q=$1 [L,QSA]
It takes you to site/private/rooms/new and it displays the content with showing /index.php
Do you think this could be what's creating the problem? The reason why it won't show site.com/gear/new might be because it doesn't display the /index.php?

Try this
RewriteEngine ON
RewriteCond %{THE_REQUEST} \ /(.*)/rooms([^\ ]+)? [NC]
RewriteRule ^ /%1/gear%2 [L,R]
RewriteRule ^(.*)/gear(/.*)?$ /$1/rooms$2 [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php?/$0 [PT,L]
Your rules don't match because
RewriteRule ^/rooms(.*)?$ /gear$1 [R]
The path never contains a leading slash (when using a RewriteRule in an .htaccess). Hence /rooms would never match.
Your rules ignore that the URL path starts with private. They match on rooms and gear as the first path directory instead.

Related

Htaccess - manage catch all in multiple directories and one single htaccess file

I'm trying to dispatch all traffic in multiple directories based on a specific keyword.
I have the following directory structure:
dotcom/
dotcom/directory1/ (with subdirs)
dotcom/directory2/ (with subdirs)
dotcom/directory3/ (with subdirs)
I have a .htaccess file located in dotcom and I would like to redirect everything behind each directory to an index file in each directory.
Example:
dotcom/directory1/anything/blabla to dotcom/directory1/index.php
dotcom/directory2/anything/blabla to dotcom/directory2/index.php
dotcom/anythingNotExisting to dotcom/index.php
Anything not in one of the existing directories should be redirected to dotcom/index.php
I tried the following for dotcom:
RewriteEngine On
RewriteCond %{ENV:HTTPS} !on
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
This catches everything
But when I tried to add conditions like the following, I get a 404:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/directory1/(.*)$ directory1/index.php?path=$1 [NC,L,QSA]
With this, if I try to access dotcom/directory1/blabla I have a 404 while if I access dotcom/directory1/ it goes to the right index.php
I have tried to use the full path dotcom/directory1/ but it doesn't help.
You may use these rules inside dotcom/.htaccess:
DirectoryIndex index.php
RewriteEngine On
RewriteCond %{HTTPS} !on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301,NE]
# ignore all rules below this for real files and directories
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
# for URIs starting with know directory paths
RewriteRule ^(directory1|directory2)/(.*)$ $1/index.php?path=$2 [NC,L,QSA]
# everything else
RewriteRule .+ index.php?path=$0 [L,QSA]
I have found something that works with the following:
RewriteEngine ON
RewriteCond HTTPS off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301,NE]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^directory1/(.*)$ /dotcom/directory1/index.php?path=$1 [NC,L,QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^directory2/(.*)$ /dotcom/directory2/index.php?path=$1 [NC,L,QSA]
RewriteEngine ON
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /dotcom/index.php?path=$1 [NC,L,QSA]
This way I'm catching everything from /directoryX/ and redirect it to the root of the directory, everything else go to dotcom

Remove the .html extension in the URL, when the folder has the same name as the .html file

I have a problem with removing .html extensions from the website address. Everything works fine until I create a folder with the name that some file already has in the given location. For example, the situation with such files:
index.html
example1.html
example1/example2.html
When I try to go from index.html to example1.html, the page does not read the extension and shows me the folder tree instead of the page. It just goes to the example1 folder instead of example1.html. Anyone can help me? I've tried most (if not all) solutions to this problem with Google and nothing works :( I leave the content of .htaccess below. Thanks in advance
SetEnv PHP_VER 5_3
SetEnv REGISTER_GLOBALS 0
RewriteEngine on
# REMOVE .HTML
RewriteCond %{THE_REQUEST} /([^.]+)\.html [NC]
RewriteRule ^ /%1 [NC,L,R]
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^ %{REQUEST_URI}.html [NC,L]
Okay, I found the answer to this question, just add these lines of code to .htaccess.
DirectorySlash Off
RewriteEngine on
RewriteCond %{THE_REQUEST} \s/+(.*?/)?(?:index)?(.*?)\.(php|html)[\s?/] [NC]
RewriteRule ^ /%1%2 [R=302,L,NE]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*) /$1.php [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^(.*) /$1.html [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !\/$
RewriteRule ^(.*) %{REQUEST_URI}/ [L,R=302]

.htaccess: Rewrite rule issue, how to redirect all to admin panel

I want to redirect my website to admin panel
Like
localhost/website/ => localhost/website/admin
localhost/website/login => localhost/website/admin
localhost/website/blog => localhost/website/admin
Here is my created rule
RewriteEngine on
RewriteCond $1 !^(index\.php)
RewriteRule ^(.*)$ /website/admin/$1 [L]
But this is not working
The following code will redirect everything to your admin page, keeping the attributes. This also applies to files who already exist !
RewriteEngine On
RewriteCond %{REQUEST_URI} !^website/admin
#RewriteCond %{REQUEST_FILENAME} !-f #OPTIONAL: Remove '#' if you want existing files to display normally
#RewriteCond %{REQUEST_FILENAME} !-d #OPTIONAL: Remove '#' if you want existing files to display normally
RewriteRule ^(.*)$ /website/admin/ [L,QSA]
Explanation:
turn on the rewrite engine:
RewriteEngine On
specific condition for rewriting: uri (the part after your domain name/localhost) cannot (! = inverse, NOT) start (^ = start of line) with website/admin = where you are rewriting to. This must be done to prevent looping.
RewriteCond %{REQUEST_URI} !^website/admin
Following 2 lines can be uncommented (remove the #) if you want that existing files are not rewritten
#RewriteCond %{REQUEST_FILENAME} !-f
#RewriteCond %{REQUEST_FILENAME} !-d
The rewrite himself:
^ = start of line
() = a substring
.* = a number of arbitrary symbols, or no symbols
$ = end of line
/website/admin = the place to rewrite to
[L,QSA] = extra options:
L=last: stop executing further lines
QSA=Query String Append: keep arguments, e.g. ?page=contact e.g.
RewriteRule ^(.*)$ /website/admin/ [L,QSA]
I hope this clarifies my solution.
Try:
RewriteEngine On
RewriteCond $1 !^website/admin
RewriteRule ^(.*)$ /website/admin [L]
You can try this with some updated as per your code:
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} ^/index\.php/component/quates [NC]
RewriteRule .* index.php/component/quotes [R=301,L]
Or Read this link:
http://coolestguidesontheplanet.com/redirecting-a-web-folder-directory-to-another-in-htaccess/
This should work:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !(website/admin)$ [NC]
RewriteRule ^(.*)$ website/admin [R=301,L]
Explanation:
First we make sure that the request is not for a file or directory that exists:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
Then we see if website/admin is exactly our requested URL (case-insensitive [NC]):
^ indicates beginning, and $ end of expression.
RewriteCond %{REQUEST_URI} !^(website/admin)$ [NC]
If all conditions are met we redirect to website/admin with HTTP status code 301 (last step [L]):
RewriteRule ^(.*)$ website/admin [R=301,L]
Try this:
RewriteEngine On
RewriteRule ^website/(?!admin).*$ website/admin [L]
RewriteRule ^website/admin[^/]+.*$ website/admin [L]

%2520 (Double space) in URL for URL with spaces

My URL structure is like
http://www.example.com/folder/index.php?dir=dir1
To be able to access it from
http://www.example.com/folder/dir1
and at the same time redirect the 1st URL to 2nd one, my htaccess (in 'folder') is
Options +FollowSymLinks
RewriteEngine On
RewriteBase /folder
RewriteCond %{ENV:REDIRECT_STATUS} 200
RewriteRule .* - [L]
RewriteCond %{QUERY_STRING} ^dir=(.*)$ [NC]
RewriteRule ^ %1? [L,R=301]
RewriteRule ^(.+)/? index.php?dir=$1 [L,QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^(.*)$ index.php?dir=$1 [L,QSA]
The trouble is that if any directory name in URL contains a 'space', the URL shows %2520 instead of the 'space'.Please advice in modifying the htaccess so it shows %20 or preferrably a simple 'space'?
try adding a NE on the redirect ie
RewriteRule ^ %1? [L,R=301,NE]
EDIT
Since you've read my htaccess, do you see any possiblity of shortening it further
Below are a couple of comments
Options +FollowSymLinks
RewriteEngine On
RewriteBase /folder
RewriteCond %{ENV:REDIRECT_STATUS} 200
RewriteRule .* - [L]
RewriteCond %{QUERY_STRING} ^dir=(.*)$ [NC]
RewriteRule ^ %1? [L,R=301]
#this looks redundant with last rule, and could be deleted?
RewriteRule ^(.+)/? index.php?dir=$1 [L,QSA]
#this says if not an existing file
RewriteCond %{REQUEST_FILENAME} !-f
#and this says if it IS an existing directory
#Is this what you wanted, or should it be not an existing directory i.e
# RewriteCond %{REQUEST_FILENAME} !-d instead
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^(.*)$ index.php?dir=$1 [L,QSA]

URL Rewrite Including Trailing Slash If Not Present

I've got this RewriteRule to work.
RewriteBase /my/path/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /my/path/index.php [L]
So URLs with a trailing slash work. http://localhost/my/path/foo/bar/
The problem is that URLs without the trailing slash will break relative links. Plus it dosen't look good.
This reaches the maximum number of internal redirects.
RewriteRule ^/my/path/(.*[^/])$ $1/ [R]
RewriteRule . /my/path/index.php [L]
And this will do... http://localhost/my/path/index.php/bar/
RewriteRule . /my/path/index.php
RewriteRule ^/my/path/(.*[^/])$ $1/ [R,L]
Any Ideas or solutions?
The confusing feature of mod_rewrite is that, after an internal redirect, even one qualified with [L], the entire set of rules is processed again from the beginning.
So you redirect a nonexistent path to index.php, but then the rules for adding a slash kick in and you don't get the result you want.
In your case you simply need to put the file nonexistence condition on both of the redirect rules:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule [^/]$ %{REQUEST_URI}/ [L,R]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ /my/path/index.php [L]
Or maybe move this condition to the top of the file:
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L] # redirect to same location to stop processing
RewriteRule [^/]$ %{REQUEST_URI}/ [L,R]
RewriteRule ^ /my/path/index.php [L]
There's also an undocumented trick to stop processing after an internal redirect which should make more complex rulesets easier to write – using the REDIRECT_STATUS environment variable, which is set after an internal redirect:
RewriteCond %{ENV:REDIRECT_STATUS} . # <-- that's a dot there
RewriteRule ^ - [L] # redirect to same location to stop processing
RewriteRule [^/]$ %{REQUEST_URI}/ [L,R]
RewriteRule ^ /my/path/index.php [L]

Resources