How to use .htaccess rewrite to completely rewrite the URL - php

I'm trying to imitate a routing effect. In my setup the login.php file is located under localhost/session/login.php. I've already found out how to omit the .php file ending but that's not enough for me. Here is my .htaccess file:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
This changes the URL to localhost/session/login but what I really need it an URL like this: localhost/login.. The trouble is that I don't really understand how the rewrite above works and I'm getting a lot of mixed answers.. don't know what to do exactly.
Is there a way to have my URL be localhost/login by using some RewriteCondition?

You may use following rules in your site domain .htaccess to enable use example.com/login rewriting to example.com/session/login.php:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/session/$1.php -f
RewriteRule ^(.+?)/?$ session/$1.php [L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^(.+)/?$ $1.php [L]

Enable mod_rewrite and .htaccess through httpd.conf and then put this code in your .htaccess.
Make sure to put this .htaccess in DOCUMENT_ROOT directory.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /session/$1 [L]
</IfModule>
Flags used are,
L - Last
This will serve the file at location localhost/session/login.php to localhost/login.php

Related

CodeIgniter - removed index.php AND .php extension using a .htaccess

Before I get slapped with a "Duplicate Question," please note that I am inquiring as to how I can add on some mod_rewrite rules to my current .htaccess with the following:
Options +FollowSymLinks
RewriteEngine on
RewriteCond $1 !^(index\.php|resources|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
My issue is, I have files outside of the application directory that are not set up with a Controller. So, how can I flat-out remove all .php extensions with my current .htaccess, WHILST retaining the ability to remove the index.php (or, index) parameter from the URL?
EDIT: I have tried another RewriteCond and RewriteRule. CodeIgniter throws a 404, though, since it thinks it is a Controller.
According to Remove .php extension with .htaccess you should add
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule !.*\.php$ %{REQUEST_FILENAME}.php [QSA,L]
to your .htaccess BUT you should add it before the CodeIgniter rules like so:
Options +FollowSymLinks
RewriteEngine on
# Serve up non-CodeIgniter PHP files if they are requested without their extension
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule !.*\.php$ %{REQUEST_FILENAME}.php [QSA,L]
# CodeIgniter rewrite rules
RewriteCond $1 !^(index\.php|resources|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
Since mod_rewrite processes and applies rules in order, you want non-CodeIgniter files to be tried first and then fall into the CodeIgniter rules.
I have no affiliation with this site and I am sure there are others like it but if you ever wanted to just test rewrite rules with various URLs then check out https://htaccess.madewithlove.be/
for removing index.php from URL in CodeIgniter. you must follow these steps.
1 . add the following code in .htaccess and .htaccess must be in the root folder.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>
2 . change $config['index_page'] = 'index.php'; to $config['index_page'] = '';
you don't need to remove .php in CodeIgniter because that not exist.
if you want to learn! so use this code to remove .php extension
add this code to .htaccess
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule !.*\.php$ %{REQUEST_FILENAME}.php [QSA,L]

how to change url rewrite by .htacces with .php extention

my website is,
http://localhost/mywebsite/page.php?id=123
using .htaccess i changes my url like this
http://localhost/mywebsite/newpostof2016
but i want like this final url
localhost/mywebsite/newpostof2016.php
current using .htacces code is
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([a-zA-Z0-9]+)$ page.php?id=$1
does this work? It should add .php to all files even if they arent php files
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.+)$ $1.php [L]
N.B. there are many reasons as to why you wouldn't want this behavior. I can't think of a case where this would benefit UX
This rule should do it.
RewriteEngine On
RewriteRule ^mywebsite/newpostof2016\.php$ /mywebsite/page.php?id=123 [L]

Remove directory name from url

Because all my files are php I have removed php extension with this .htacess file:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
So I have this URL:
http://example.com/live/kanal/some-title-1
http://example.com/live/kanal/some-title-2
http://example.com/live/kanal/some-title-13
http://example.com/live/kanal/some-title-45
http://example.com/live/kanal/2some-title-333
etc ...
I want to hide kanal/ from url, to get URL like this:
http://example.com/live/some-title-1
http://example.com/live/some-title-2
http://example.com/live/some-title-13
http://example.com/live/some-title-45
http://example.com/live/2some-title-333
How can I achieve this using mod_rewrite?
I used online generators for doing the rewrite rules but none can remove a piece of text from the URL. Also I made search on this forum, and I found some posts about mod rewrite directory but I have issues when pasting in my .htacess code.
You can use this code in /live/.htaccess:
RewriteEngine On
RewriteBase /live/
RewriteRule ^(?!kanal/)(.*)$ kanal/$1 [L,NC]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.+?)/?$ $1.php [L]
Make sure there is no .htaccess in kanal/ sub-directory.

.htaccess file doesn't work properly with path_info and/or get method

Fixed it. Did a small change on the file. It can be seen below.
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*) index.php?$1 [NC,QSA,L]
I have been trying for a whole day to fix an issue I'm having. I have this .htaccess file which simply rewrites the urls just like
http://example.com/about/?req=true
http://example.com/index.php/about/?req=true
It works on my localhost without a problem, but when I upload it to the server I get this "no input file specified" error. You can see the content of the .htaccess file below.
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*) index.php/$1 [L]
I tried different things I found on the internet including adding a php.ini file which sets cgi.fix_pathinfo to 1, but none of them worked except one and that one doesn't read the request string. You can see that one below.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*?)/?$ /index.php?x=$1 [L]
Thanks in advance.
First off all check whether rewrite module is enable in apache or not
if enable then u can use this code hide index.php with in url
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /base_directory_name/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /base_directory_name/index.php [L]
</IfModule>
I think this will help u .

htaccess - using codeigniter framework

I search a lot websites and lots of links and i follow the same for enable htacces in wamp but facing the problem and now as default use index.php even when i run the page without index.php its not working even msg comes page not found on server i already enable in apache module also the httpd.conf file remove the # sign from rewrite module and i use the mention below code in htaccess file for codeigniter for suggestions i share htaccess code
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]
please suggest so i can solve this issue on wamp server
Try this code in your htaccess :
RewriteEngine On
RewriteBase /my_project_name/
# Do not enable rewriting for files or directories that exist
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# Rewrite to index.php/URL
RewriteRule ^(.*)$ index.php/$1 [PT,L]
Try this
DirectoryIndex index.php
RewriteEngine on
RewriteCond $1 !^(index\.php|images|css|js|robots\.txt|favicon\.ico)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ ./index.php/$1 [L,QSA]

Categories