Flat links with htaccess - regex

What code can I use to have flat links with htaccess?
An example of what I want:
site.com/folderA becomes : site.com/index.php?section=folderA
site.com/folderA/folderB becomes : site.com/index.php?section=folderA&action=folderB
site.com/folderA/folderB/folderC becomes : site.com/index.php?section=folderA&action=folderB&id=folderC
I tried it with the following code:
Options +FollowSymLinks
RewriteEngine On
RewriteRule ^([^/]+) index.php?section=$1 [NC]
RewriteRule ^([^/]+)/([^/]+) index.php?section=$1&action=$2 [NC]
RewriteRule ^([^/]+)/([^/]+)/([^/]+) index.php?section=$1&action=$2&id=$3 [NC]

Make sure to use $ (line end anchor) to avoid matching unwanted URL parts:
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
# if request is not for a file/directory
RewriteCond %{SCRIPT_FILENAME} -d [OR]
RewriteCond %{SCRIPT_FILENAME} -f
# then skip from rewrites
RewriteRule ^ - [L]
RewriteRule ^([^/]+)/?$ index.php?section=$1 [NC,L,QSA]
RewriteRule ^([^/]+)/([^/]+)/?$ index.php?section=$1action=$2 [NC,L,QSA]
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/?$ index.php?section=$1action=$2id=$3 [NC,L,QSA]

Related

HTACCESS Treat important directory as a parameter

I want directory www.example.com/core to be translated into string, instead of just dissalowing access to it. is that possible?
UPDATE (STILL NO LUCK):
My current .htaccess
#Options -Multiviews
RewriteEngine On
#Remove the comments below to enable enforcing HTTPS
RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
# Handle Front Controller...
RewriteBase /public
RewriteCond %{REQUEST_FILENAME} !-f
#RewriteCond %{REQUEST_FILENAME} !-d
#RewriteRule ^(.*)$ launcher.php?urls=$1 [QSA,L]
RewriteRule . launcher.php [L]
Here are some scenarios:
I write a url www.example.com/some-dir-that-does-not-exist and it works fine.
I write a url www.example.com/url-that-DOES-exist and the browser redirects it to www.example.com/url-that-DOES-exist/?url=url-that-DOES-exist
That is due to mod_dir module adding a trailing slash in front of real directories and making a 301 redirect after your rewrite rule.
To fix have it like this:
DirectorySlash Off
RewriteEngine On
RewriteBase /public/
RewriteCond %{THE_REQUEST} /launcher\.php [NC]
RewriteRule ^ - [F]
#Remove the comments below to enable enforcing HTTPS
RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
# add a trailing slash to directories
RewriteCond %{DOCUMENT_ROOT}/$1 -d
RewriteRule ^(.*?[^/])$ %{REQUEST_URI}/ [L]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*?)/?$ launcher.php?urls=$1 [QSA,L]
Your code will become:
#Options -Multiviews
RewriteEngine On
#Remove the comments below to enable enforcing HTTPS
RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteBase /public
# Treat existing /core directory as non-existing (handled by launcher.php)
RewriteCond %{REQUEST_URI} ^/core(/.*)?$
RewriteRule . launcher.php [L]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-f
#RewriteCond %{REQUEST_FILENAME} !-d
#RewriteRule ^(.*)$ launcher.php?urls=$1 [QSA,L]
RewriteRule . launcher.php [L]

How to modrewrite in my htaccess?

Why my .htacces is not working? I have got pages:
site.com/page.php
I want to see pages, like this:
site.com/page/
htacces here:
RewriteEngine On
RewriteBase /
RewriteRule /(.*).php /$1
Also I want to see redirect from page.php to /page/ too
How?
You can use this code in your DOCUMENT_ROOT/.htaccess file:
RewriteEngine On
RewriteBase /
# To externally redirect /dir/file.php to /dir/file
RewriteCond %{THE_REQUEST} \s/+(?:index)?(.*?)\.php[\s?] [NC]
RewriteRule ^ /%1 [R=301,L,NE]
# To internally forward /dir/file to /dir/file.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1\.php -f [NC]
RewriteRule ^(.+?)/?$ $1.php [L]

hide folder using .htaccess with subfolders

I have been trying to figure out this for a while but no success-
I have this site structure http://example.com/catalog/current/sub-folders/..
The result should hide the folder "current" so that the paths look like http://example.com/catalog/sub-folders/
This is what I have so far-
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteCond %{REQUEST_URI} !/$
RewriteRule (.*) $1\.php [L]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+current/([^\s]+) [NC]
RewriteRule ^ %1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule (?!^current/)^(.*)$ /current/$1 [L,NC]
when I place this .htaccess to the root and go to http://example.com/catalog/sub-folders/, it try to look for /current/catalog/sub-folders/
Any help to approach this problem will be highly appreciated.
Keep your code like this:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteCond %{THE_REQUEST} \s/+(catalog)/current/(\S*) [NC]
RewriteRule ^ %1/%2 [R=301,L,NE]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(catalog)/((?!current/).*)$ $1/current/$2 [L,NC]
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteCond %{REQUEST_URI} !/$
RewriteRule ^(.+)$ $1.php [L]
Try making the last rule:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule (?!^hsc-bulletin/)^(.*)$ /current/$1 [L,NC]
This instead:
RewriteCond %{REQUEST_URI} !^/current/
RewriteRule ^(?!hsc-bulletin/)(.*)$ /current/$1 [L,NC]

.htacces without trailing slash redirect to with trailing slash

I'm using this .htaccess with this inside:
DirectoryIndex index.php index.html
DirectorySlash On
Options -Indexes
Options +FollowSymlinks
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www.(.+)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
RewriteCond %{REQUEST_URI} ^/(page1|page2|page3)$
RewriteRule ^(.*)$ http://%{HTTP_HOST}/$1/ [R=301,L]
#Show every html,htm and xml as php
RewriteRule ^(.*)\.html$ $1.php [nc]
RewriteRule ^(.*)\.htm$ $1.php [nc]
RewriteRule ^(.*)\.xml$ $1.php [nc]
but i did not get to redirect http://example.com/page1 to -> http://example.com/page1/ (and the others page2 and page3 ). since i have test in local my url will be
localhost/example/page1
and should redirect to
localhost/example/page1/
does any one see any problem?
For adding a trailing slash you can have your rules like this:
DirectoryIndex index.php index.html
DirectorySlash On
Options -Indexes
Options +FollowSymlinks
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^(.*)$ http://%1/$1/ [R=301,L,NE]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(page1|page2|page3)$ /$1/ [R=301,L,NC]
#Show every html,htm and xml as php
RewriteRule ^(.+?)\.(?:xml|html?)$ $1.php [NC,L]

.htaccess to remove .html causing redirect loop

My .htaccess file is creating a redirect loop. I have searched online and tried lots of varieties including many from this site but still can't get it to work. The purpose of it is to remove the .html from the end of the URL.
Here is my code:
RewriteEngine On
RewriteCond %{SCRIPT_FILENAME}/ -d
RewriteCond %{SCRIPT_FILENAME}.html !-f
RewriteRule [^/]$ %{REQUEST_URI}/ [R=301,L]
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^(.+)\.html$ /$1 [R=301,L]
RewriteCond %{SCRIPT_FILENAME}.html -f
RewriteRule [^/]$ %{REQUEST_URI}.html [QSA,L]
Replace all of your code with this:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
## hide .html extension
# To externally redirect /dir/foo.html to /dir/foo
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s(.+?)\.html?[/?\s] [NC]
RewriteRule ^ %1? [NE,R=301,L]
# To internally forward /dir/foo to /dir/foo.html
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1.html -f
RewriteRule ^(.+?)/?$ /$1.html [L]
## append trailing slash if needed
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule [^/]$ %{REQUEST_URI}/ [R=301,L]