make seo friendly url using .htaccess - php

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...

Related

PHP Pretty URLs combination of two mod_rewrite rules?

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

.htaccess conflict between two rewrite rule

in my website two rewrite rule are conflicting with each other
i have url for my users like= http://twekr.com/sam
and all my website pages open like http://twekr.com/login
now here is htaccess file rules
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
RewriteRule ^([a-zA-Z0-9_-]+)$ profile.php?user=$1
RewriteRule ^([a-zA-Z0-9_-]+)/$ profile.php?user=$1
Now only this rule works because its loaded first =
RewriteCond %{REQUEST_FILENAME} !-d
And if i put profile rewrite rule above
RewriteRule ^([a-zA-Z0-9_-]+)$ profile.php?user=$1
RewriteRule ^([a-zA-Z0-9_-]+)/$ profile.php?user=$1 (then this works)
and this doesnt
RewriteCond %{REQUEST_FILENAME} !-d (does not work)
What is the problem in htaccess rule?
You should check for presence of .php file before adding .php extension:
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+?)/?$ $1.php [L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([\w-]+)/?$ profile.php?user=$1 [L,QSA]

Htaccess over right a RewriteRule for some files

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]

How to load php file without php extension

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]

htaccess redirect virtual directory

Can't figure out what I'm doing wrong.
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(.+)\.php$ detail.php?name=$1 [NC,QSA,L]
RewriteRule ^(.+)/directory/\.php$ detail2.php?name=$1 [NC,QSA,L]
The first RewriteRule should redirect anything (ending on php like domain.com/product1.php) from the root domain to detail.php (it can not affect things like domain.com/contact.php)
A 2nd RewriteRule should redirect anything from domain.com/directory/product-b1.php to detail2.php
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* /foldernamehere/index.php/$0 [PT,L]
paste this code in your .htaccess file it will resolve your problem

Categories