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]
Related
I had added this code in .htaccess for removing .php extension from the url's.Also I had written a code for making blog url seofriendly like https://url.com/posttitle
<IfModule mod_rewrite.c>
#removing extension from url
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
#blog
#if the file with the specified name in the browser doesn’t exist, or the directory in the browser doesn’t exist then procede to the rewrite rule below
RewriteCond %{REQUEST_FILENAME} !-d [NC]
RewriteCond %{REQUEST_FILENAME} !-f [NC]
RewriteRule ^(.*)$ blog_detail.php?id=$1 [QSA,L]
</IfModule>
I had added links like Home.
Home
How to remove index from url in this case?
You can simply use a "/" in your href attribute when navigating to your root (index.html/.php)
Hello.
I have a site protected by a .htaccess which redirects everything to the /pb directory except the /cron directory which is directly accessible (later it will be protected).
My htaccess works (it redirects as I want) BUT I cannot load the .css .js files that I call directly into my index page.
Do I have to put a new .htaccess in my /pb directory to allow access to .css or can I do it in my root htaccess ? I do not understand.
My .htaccess file (the https lines can't be removed "hostinger")
RewriteEngine on
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} (www\.)?XXXXXXXXX.net
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteRule ^(cron) - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^pb/ - [L]
RewriteCond %{DOCUMENT_ROOT}/pb/$1 -f
RewriteRule (.+) pb/$1 [L]
RewriteRule (.*) pb/index.php?path=$1 [L,QSA]
order deny,allow
deny from all
allow from XX.XXX.XXX.XX
Thanks for your help
First thing first, I have fixed your htacces rules file, comments have been made inside Rules file. Have your htaccess rules file like as follows style, make sure its placed in same folder where pb folder is there. Also please do clear your browser cache before testing your URLs.
Options -Indexes -MultiViews
RewriteEngine on
##Apply https rules here.
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^(?:www\.)?XXXXXXXXX.net [NC]
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [NE,L,R=301]
##Handling base URI of site.
RewriteRule ^/?$ pb/ [R=301,L]
##Blocking pb uri pages non existing ones.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^pb/ - [NC,L]
##making all uris which are files to pb folder.
RewriteCond %{DOCUMENT_ROOT}/pb/$1 -f
RewriteRule ^(.+)/?$ pb/$1 [L]
##Making non existing uris to index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/?$ pb/index.php?path=$1 [L,QSA]
order deny,allow
deny from all
allow from XX.XXX.XXX.XX
JS/CS rewrite/redirect:
You may need to use base tag to fix your js and other relative resources. If you are linking js files using a relative path then the file will obviously get a 404 because its looking for URL path. for example if the URL path is /file/ instead of file.html then your relative resources are loading from /file/ which is not a directory but rewritten html file. To fix this make your links absolute or use base tag. In the header of your webpage add this <base href="/"> so that your relative links can load from the correct location.
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.
When I don't edit my .htaccess file, my image path reads something like: http://this.website.com/codeigniter/inc/images/logo.jpg.
However, as soon as I add this code into the .htaccess file:
RewriteEngine On
RewriteCond $1 !^(index\.php)
RewriteRule ^(.+)$ index.php?$1 [L]
(to remove the index.php from the URL), I get 404 error, even with the exact same path.
How do I remove index.php and still have access to my images?
RewriteEngine On
RewriteCond $1 !^index\.php [NC]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+)$ index.php?$1 [L]
The two new RewriteConditions would exclude existing directories and files (and so images) from redirection.
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