I have this working htaccess
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^test/([0-9]+)/?$ test.php?id=$1 [L]
my question for this if I have that rule on my htaccess how can I load php file that have no .php extension.
I add this to my htacess
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
It works fine http://localhost/myprojectfolder/account
but this will not work anymore http://localhost/myprojectfolder/1
how can I make them both working ?
Thank you in advance.
Try adding another condition to your php extension rule:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
that ensures that when you add the .php extension, it actually points to a file that exists.
If you are using this URL http://localhost/myprojectfolder/1 as in your other question. Then test shouldn't even be in the first rule. Then you can just make sure the file exists.
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([0-9]+)/?$ test.php?id=$1 [L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
Related
I have this .htaccess code in the root folder to replace this url: localhost/mysite/profile.php?username="someone" to become localhost/mysite/someone.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /mysite/profile.php?username=$1
This is working fine but what I want is to include subpages like for example the about page to be localhost/mysite/about instead of localhost/mysite/about.php. And the subpages are in the root folder too.
How can I achieve these two rewrites?
You can have this code in mysite/.htaccess:
RewriteEngine On
RewriteBase /mysite/
# add .php internally to files
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+?)/?$ $1.php [L]
# handles profile URLs
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([\w-]+)/?$ profile.php?username=$1 [L,QSA]
Try below code it's working fine to me.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
Try This
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^mysite/([A-Za-z0-9-]+)$ /mysite/profile.php?username=$1
I'm trying to make SEO url using .htaaccess
this is my .htaccess :
AddDefaultCharset utf-8
Options +FollowSymLinks
RewriteEngine On
RewriteRule ^ads/([0-9]+) advertisement.php?ad_id=$1 [NC,L]
RewriteRule ^rss/([0-9]+)-([^/]+) rss.php?cat=$1 [NC,L]
#replace template folder with a fake folder named theme
RewriteRule ^theme/(.*) template/$1 [NC,L]
#replace /core/ajax folder with a fake folder named ajax
RewriteRule ^a/(.*) core/ajax/$1 [NC,L]
RewriteRule ^news/([0-9]+)-([0-9]+)$ direct.php?cat=$1&post_id=$2 [NC,L]
RewriteRule ^(.*)/([0-9]+)-([^/]+)$ direct.php?cat=$1&post_id=$2 [NC,L]
RewriteRule ^(.*/)page/([0-9]+)$ category.php?cat=$1&p=$2 [NC,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ category.php?cat=$1 [NC,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(([A-Za-z0-9\-]+/)*[A-Za-z0-9\-]+)?$ $1.php [NC,L]
i want to call php files directly without .php and if the file doesnot exist send request to category.php like you see above both are ok and work fine separately when i put them in same file they don't work.
example :
last-news.php is a existing file, and sport don't exist
i want to be like this
1. http://example.com/last-news open http://example.com/last-news.php
2. http://example.com/sport open http://example.com/category.php?cat=sport
right now number 2 works but i can't make number 1 work as well
can anyone here help me ?
solved the problem myself,code is right but placement is wrong
simply just replaced
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ category.php?cat=$1 [NC,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(([A-Za-z0-9\-]+/)*[A-Za-z0-9\-]+)?$ $1.php [NC,L]
with :
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(([A-Za-z0-9\-]+/)*[A-Za-z0-9\-]+)?$ $1.php [NC,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ category.php?cat=$1 [NC,L]
and works fine...
This is my current .htaccess file:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]*)$ /page.php?page=$1 [L]
RewriteRule ^([^\.]+)$ $1.php [NC,L]
So right now it rewrites the page.php to the root of the site. I want this to be the case, except for when there is a file in the root of the domain, then I want it to show that file instead.
It does this automatically already except you have to include the .php on the end, so like: http://domain.com/contact.php
I want it like: http://domain.com/contact
The last line of the htaccess code does that except it will only work in subfolders.
Try this
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]*)$ /page.php?page=$1 [L]
I have the following rule for removing php extensions
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^([^\.]+)$ en/$1.php [NC,L]
Now with these rules it works when I access domain.com/abc .. But the problem is that, when I manually access domain.com/abc.php .. it also works.
What I want is that domain.com/abc.php should also redirect to domain.com/abc.
How can I achieve this ?
In short, in any case I want php extension to be removed.
You can add Rule, which will redirect all *.php to uri-s without extension:
RewriteRule ^(.*).php$ /$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^([^\.]+)$ en/$1.php [NC,L]
i'm working a small project in which i'm using .htaccess to make URLs short and sweet. But it's not working. Here's the code:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php #removing .php extension
RewriteCond %{SCRIPT_FILENME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^category/(.+)$ category.php?category=$1 #removing get variables
By using the first condition it's removing the use of .php extension in the URL bar. And the second condition is added with an intention to remove the GET variables. URL is something like this:
www.example.com/category.php?category=latest
and i want this url to be look like this:
www.example.com/category/latest
Flip the order otherwise first rule will catch this category URI also.
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^category/(.+)$ category.php?category=$1 [L,QSA]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)$ $1.php [L]