.htaccess redirect everything on root folder - php

On my sites, when type www.abc.com/folder, it's redirect to index.php. And I want the script don't touch on others folder and files. So is there anyway to prevent this?
This is the .htaccess file:
RewriteEngine on
RewriteCond %{request_filename} -f
RewriteRule ^(.*) $1 [L]
RewriteRule ^([a-z]+)(/([^/]{0,32})(/.+)?)?$ index.php?a=$1&q=$3 [L]

To redirect everything from root only to home page you can use this rule:
RewriteEngine On
RewriteRule ^[^/]+/?$ / [L,R]
RewriteCond %{request_filename} -f
RewriteCond %{request_filename} -d
RewriteRule ^([a-z]+)(/([^/]{0,32})(/.+)?)?$ index.php?a=$1&q=$3 [L,QSA]

Related

htaccess rewrite rule remove subdirectory and .php extension

I need to rewrite the url of my web and make it hide subfolder and the .php extension, like this:
original : http://192.168.100.5/project/controller/report.php?unit=1&year=2021
goal : http://192.168.100.5/project/report?unit=1&year=2021
My current rules which doesnt work
Options -Indexes
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)$ controller/$1.php [L]
How to achieve this? thankyou
You will need 2 .htaccess files.
site root .htaccess:
RewriteEngine On
# forward everything to controller/ if not a file or directory
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* controller/$0 [L]
controller/.htaccess:
RewriteEngine On
# add .php extension internally
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*?)/?$ $1.php [L]

How to display a login form '/login' instead of 'login.php' or 'login.html'

How to make my websits login form page to be "/login" instead of "/login.php" or "/login.html" Id like my website to only display the directory " /login" . Any replies and tips are well recieved. thanks.
A simple solution would be to rename your "login.php" to "index.php" and place it in it's own folder called "login"
URL rewriting with PHP
There is existed answer that can solve you problem.
Use URL rewrite login into login.php
I accomplish this with .htaccess -- This allows http://exmaple.com/whatever.php to be seen as simply http://example.com/whatever:
Options -MultiViews
RewriteEngine on
# skip all files and directories from rewrite rules below
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]
# add php if possible
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^ %{REQUEST_URI}.php [L]
# rewrite other to index.php file
RewriteRule ^ /index.php [L]
To remove the .php and .html extension in the url and make website.com/login.php or website.com/login.html display as website.com/login add the following code inside your .htaccess file:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)$ $1.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^(.*)$ $1.html
</IfModule>
(Not tested).

Removing .php extensions from files in htdocs

I have my project in my htdocs folder where my main file is index.php. I have tried multiple time to remove the .php extension when the file is opened in the browser, using the .htaccess file with the following lines without luck:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule !.*\.php$ %{REQUEST_FILENAME}.php [L]
The extension remains. Is it because the link in my browser is
file:///C:/wamp64/bin/apache/apache2.4.23/htdocs/Cipcpr/index.php
and not
localhost/...
You can use:
Options -MultiViews
RewriteEngine on
# remove php
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R=301,L]
# rewrite with php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^(.+)/?$ $1.php [L]
If your project doesn't need to remove extensions dynamically, you can manually set the RewriteRules for every file. Here is the code for your index.php
RewriteEngine On
RewriteRule ^index/?$ index.php [NC,L]
when you now type /index it will reference to /index.php file.
To add other file (let's say login.php) you can just copy last RewriteRule and rewrite the indexes.
RewriteRule ^login/?$ login.php [NC,L]
Just add .htaccess file to the root folder of your site(for example, /home/domains/domain.com/htdocs/) with following content:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php

How to create personalized urls in php?

I want to convert an URL like this:
http://somesocial.com/SomeSocial/success/profile?search_user=exampleuser01
Into a personalized URL like this: http://somesocial.com/exampleuser01
I've tried creating a folder with a file inside success/preview/user_preview.php
which is a copy of profile.php to get the parameter from 'search_user' with $profile_id = $_GET['profile_id'] and header('Location:'.'success/profile?'.$profile_id); To redirect the searched user for his original link.
Modified my .htaccess to:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !^success/preview/user_preview?search_user=
RewriteRule ^/?([^/]+)$ success/preview/user_preview?search_user=$1 [L]
This works perfectly, but every time I try to access a file from the main dir (i.e Index.php) it redirects me to http://somesocial.com/SomeSocial/success/profile.php?search_user=Index.php
Why?
Try reordering your rule wit more restrictive conditions:
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.+?)/?$ $1.php [L]
RewriteRule ^([^/]+)/?$ success/preview/user_preview?search_user=$1 [L,QSA]

Mod Rewrite load index.php on bad path

These are the rewrite rules I'm using. At this point I'm trying to force bad urls to load index.php. The html tag <base href="/directory-name/" /> is in the top of index.php, so my css, images and other assets are loading properly.
Options -Indexes +FollowSymLinks
RewriteEngine On
RewriteCond ${REQUEST_URI} ^.+$
RewriteCond %{REQUEST_FILENAME} \.(gif|jpe?g|png|js|css|swf|php|ico|txt|pdf|xml)$ [OR]
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -l
RewriteRule ^ - [L]
RewriteRule ^(.*)/(.*)/(.*) /index.php?x=$1&y=$2&z=$3 [L]
RewriteRule ^(.*)/(.*) /index.php?x=$1&y=$2 [L]
RewriteRule ^(.*) /index.php?x=$1 [L]
Url Examples:
www.domain.com/directory-name/
www.domain.com/directory-name/index.php
www.domain.com/directory-name/index.php/arg1/arg2/arg3
www.domain.com/directory-name/anything-here
www.domain.com/directory-name/anything-here/arg1/arg2/arg3
The first 3 url examples work fine and load index.php. But the 4th and 5th urls fail and dumps me back to the html root / where a xampp index exists. I'm assuming the failure of the last url rewrite is because the index.php is in a sub-directory off of root?
How would I fix that? How would I force the urls to load www.domain.com/directory-name/index.php even if something else is in the path?
Try these rules:
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/?$ index.php?x=$1&y=$2&z=$3 [L]
RewriteRule ^([^/]+)/([^/]+)/?$ index.php?x=$1&y=$2 [L]
RewriteRule ^([^/]+)/?$ index.php?x=$1 [L]

Categories