.htaccess URL Rewrite file extension and front controller using mod_rewrite - .htaccess

I currently have a file that is removing .php from the URL to make it neat and much better for SEO.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
I have a page called Main.php and I display other pages using Main.php?Page=page1.php. I want to add a rule to my .htaccess file that will still allow my remove .php rule and make the Main.php?Page=page1.php display as
URL.com/page1
Is this possible? When I added the following line to my file my main page started loading in a loop, when I removed the line it worked fine.
RewriteRule ^(.*)$ Main.php?Page=$1.php [QSA,L]
Have I written this line wrong and caused it to loop?

RewriteRule ^(.*)$ Main.php?Page=$1.php [QSA,L]
You need to make sure you are not already on Main.php before rewriting the URL. That's what is current resulting in the loop... Main.php?Page=Main.php.php&Page=Main.... etc.
Try something like:
# (1) If requesting a ".php" file (including "Main.php")
# or any known static resource then stop here...
RewriteRule \.(php|css|js|jpe?g|png|gif)$ - [L]
# (3) Otherwise, if the request doesn't map to an existing file then rewrite to Main.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*) Main.php?Page=$1.php [QSA,L]
Remove the QSA flag if you are not passing query strings on the original request.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
Your earlier directives that append the .php extension should be checking that appending a .php will result in a valid request, otherwise this will append .php to all your non-php requests and never reach Main.php. Something like:
# (2) Append ".php" if there is no extension
# but only if appending ".php" would result in a valid request
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^([^.]+)$ $1.php [L]
No need to escape the dot in a character class. And the NC flag is not necessary here.
Summary: So, bringing this together we have:
# (1) If requesting a ".php" file (including "Main.php")
# or any known static resource then stop here...
RewriteRule \.(php|css|js|jpe?g|png|gif)$ - [L]
# (2) Append ".php" if there is no extension
# but only if appending ".php" would result in a valid request
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^([^.]+)$ $1.php [L]
# (3) Otherwise, if the request doesn't map to an existing file then rewrite to Main.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*) Main.php?Page=$1.php [QSA,L]
UPDATE#2: Only append .php to Login, index and Signup, instead of any file that exists. Everything else (including non-existent files) get rewritten to Main.php.
# (2) Append ".php" to select requests
# but only if appending ".php" would result in a valid request
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^(Login|index|Signup)$ $1.php [L]
# (3) Otherwise, rewrite everything else to Main.php
RewriteRule (.*) Main.php?Page=$1.php [QSA,L]

Related

Need help for .htaccess RewriteRule

I've been trying to setup a RewriteRule for my .htaccess to redirect any not found .php url to my loader.php ... For some reasons, it's redirecting even if the file exist.
Here's what I have:
# Engine On for Extension Removal
RewriteEngine On
# Rewrite the url for the login page
RewriteRule ^login/?$ index.php?tab=login [NC,L]
# Load the url with the page loader if the url doesn't match any of the rule above
RewriteRule ^(.*)\.php$ loader.php?page=$1 [NC,L]
# Remove PHP Extension
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+?)/?$ $1.php [L]
Everything is working before I added this line:
RewriteRule ^(.*)\.php$ loader.php?page=$1 [NC,L]
Right now, no matter what is in my link, even if it exist or not, it'll load the loader.php. And I have other rules like the login one, same thing but different name which is why I didn't bother posting them.
Try
# Engine On for Extension Removal
RewriteEngine On
# Rewrite the url for the login page
RewriteRule ^login/?$ index.php?tab=login [NC,L]
# Remove PHP Extensio
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.+?)/?$ $1.php [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)\.php$ loader.php?page=$1 [NC,L]
The problem with
RewriteRule ^(.*)\.php$ loader.php?page=$1 [NC,L]
is that it accepts any .php url in the request and rewrites it to loader.php.
The condition
RewriteCond %{REQUEST_FILENAME} !-f
change this behaviour of the rule, it tells server to process the rule only when there is a request for non existent php file.

Remove .php extension from url

I am required to remove .php extension and query string as well from url by rewriting the url
I know this can be done in .htaccess file
I have this in my .htaccess file so far
RewriteEngine on
DirectoryIndex Home.html
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteCond %{REQUEST_URI} !/$
RewriteRule (.*) $1\.php [L]
# if a directory or a file exists, use it directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# otherwise forward it to index.html
RewriteRule .* Home.html
now the url
example.com/blog.php?post=12&comment=3
can be accessed by
example.com/blog.php/post/12/comment/3
but i want to remove .php extension as well
this must be accessible through this url
example.com/blog/post/13/comment/3
Any help?
Thank you in advance.
Searches for everything except . after /
RewriteRule ^(([^/]+/)*[^.]+)$ /$1.php [L]

htaccess - removing preceding index.php but retain some parameters

For example I have this URL: http://www.domain.com/index.php?mod=category&continent=Asia
I wanted to truncate the URL by removing the index.php, so the output URL would be: http://www.domain.com/?mod=category&continent=Asia
Is there any alternative(preferably shorter) code than this one:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !(index.php) [NC]
RewriteRule ^(.*)$ blah-blah-blah [L]
I think that what you are saying is that you want your external URIs of the form
http://www.domain.com/?mod=category&continent=Asia
to be processed by DOCROOT/index.php
Specifying a DirectoryIndex of index.php will achieve this as will
RewriteRule ^$ index.php
The rewrite engine will automatically append the query string.
To truncate the site URIs in your web pages, you need to change the HTML.

HTACCESS | Social Engine | Stop redirecting Root index

I have a install of SocialEngine currently and all urls are processed by .htaccess file that redirects through to the socialengine.php file and processes nice URLs etc.
I am wanting to stop example.com/ been redirected through to the script as I am wanting to put a different index page there. So I want my .htaccess file to process every other file it cant find through the socialengine.php script but keep the root file going to index.php.
So if it’s .com/ it goes to index.php and if it’s anything else it’s processed through the socialengine.php.
Below is my .htaccess file that requires alteration.
<IfModule mod_rewrite.c>
Options +FollowSymLinks
RewriteEngine On
# Get rid of index.php
RewriteCond %{REQUEST_URI} /socialengine\.php
RewriteRule (.*) socialengine.php?rewrite=2 [L,QSA]
# Rewrite all directory-looking urls
RewriteCond %{REQUEST_URI} /$
RewriteRule (.*) socialengine.php?rewrite=1 [L,QSA]
# Try to route missing files
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} public\/ [OR]
RewriteCond %{REQUEST_FILENAME} \.(jpg|gif|png|ico|flv|htm|html|php|css|js)$
RewriteRule . - [L]
# If the file doesn't exist, rewrite to index
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ socialengine.php?rewrite=1 [L,QSA]
</IfModule>
Thanks in advance for any replies.
Put the code below straight after RewriteEngine On.
RewriteCond %{REQUEST_URI} ^/$
RewriteRule .* - [L]
Explanation: The first line matches a request to http://example.com/ (REQUEST_URI contains / in this case). The second line passes the request untouched (note the -)
References:
http://httpd.apache.org/docs/current/mod/mod_rewrite.html#rewriterule

htaccess redirect problems

My .htaccess looks like this:
RewriteEngine on
RewriteCond %{REQUEST_URI} !(\.gif)|(\.jpg)|(\.png)|(\.css)|(\.js)|(\.php)|(\.swf)|(\.xpi)|(\.ico)|(\.src)$
RewriteCond %{REQUEST_URI} ^(.*)$
#RewriteCond %{REQUEST_FILENAME} ! -f
RewriteRule (.*)$ view.php?picid=$1 [L]
Problem is that when I visit www.example.com, it’s sending me to view.php. There is an index.php file in my root directory. How do I get .htaccess to ignore the index file?
Try this rule:
RewriteCond %{REQUEST_URI} !.*\.(gif|jpg|png|css|js|php|swf|xpi|ico|src)$ [OR]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule (.+) view.php?picid=$1 [L]
The important changes I made:
The regular expression in your first RewriteCond is faulty. The $ anchor is only applied to the last option in the alteration.
The second RewriteCond directive is useless.
The third RewriteCond that’s commented out will do just what you want: Check if the requested URI path can be mapped to an existing file.
Altered the quantifier from zero or more to one or more characters. That will exclude the empty URI path when / is requested.
Remove the # from the fourth line and remove the space between the exclamation mark (!) and the -f
RewriteCond %{REQUEST_FILENAME} !-f
This line says that if the file doesn't already exist on the server, in this case, index.php, then continue and do the rewrite that follows.
Also check you've got both a DirectoryIndex as well as checking against any valid directories.
DirectoryIndex index.php
RewriteCond %{REQUEST_FILENAME} !-d
Which would leave you with this cleaner version:
DirectoryIndex index.php
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*)$ view.php?picid=$1 [L]
This first sets up index.php as the default file. Then you run the Rewrite engine, and if the requested file does not already exist on the server as either a file or a directory, will pass things on to the view.php file and run its magic.

Resources