How can I make the URL search engine friendly? - php

I want to cahange mysite.com/profile.php into mysite.com/profile
I wrote the htacess file as
RewriteEngine On
RewriteRule ^profile/?$ profile.php [NC,L]
and placed in root directory. It doesn't work. I use the apache server. What is wrong with my program?

Try something like this instead:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
That way you dont have to do it for each individual URL.
credit

Related

How to use .htaccess rewrite to completely rewrite the URL

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

htaccess Rewrite directory to php pages

Say I have these pages stored in the root:
page1.php
page2.php
how do I rewrite this:
domain.com/page1/
to:
domain.com/page1.php
P.S.Also need to make sure the rewritten url does not clash with a real directory.
UPDATE:
Solutions below were only working on my remote server, but not localhost Xampp server.
I added this first line to my htaccess and solutions below worked on my local server!
Options +FollowSymLinks +MultiViews
You may use this rule in your site root .htaccess:
RewriteEngine On
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^(.+?)/?$ $1.php [L]
You can use the following Rule in /root/.htaccess :
RewriteEngine On
#skip real files and directories
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# rewrite /file/ to /file.php if /file/ exists as .php
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+)/?$ /$1.php [L]
If you just want to redirect a small number of pages, you could do this:
RewriteRule ^page1/?$ /page1.php [NC,L]
RewriteRule ^page2/?$ /page2.php [NC,L]
You might want to consider something like the following, though, where you pass the number as an argument (assuming you are following a naming convention as you described).
^page/([0-9]+)/?$ /pageLoader.php?p=$1 [NC,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.

hide extension .php in url mod_rewrite

I have a website in PHP, and I'm trying to hide the extension. I've found a couple of things via Google, but they all seem to be too complicated, or they redirect index to index.php: like if you write name.com/contact it goes to name.com/contact.php. I don't want that, I just want:
www.name.com/es/index.php to be www.name.com/es/inicio
www.name.com/es/empresa.php to be www.name.com/es/empresa
www.name.com/es/productos.php to be www.name.com/es/productos
www.name.com/es/clientes.php to be www.name.com/es/clientes
www.name.com/es/recetas.php to be www.name.com/es/recetas
www.name.com/es/contacto.php to be www.name.com/es/contacto
www.name.com/en/index.php to be www.name.com/en/home
www.name.com/en/company.php to be www.name.com/en/company
www.name.com/en/products.php to be www.name.com/en/products
www.name.com/en/clients.php to be www.name.com/en/clients
www.name.com/en/recepis.php to be www.name.com/en/recepis
www.name.com/en/contact.php to be www.name.com/en/contact
the /en/ folder is English version and the /es/ folder is the Spanish version
Also the links inside the website, would be also without the extension.
E.g, an image linked to clientes would be clientes and not clientes.php and if somebody tries to go to /page.php, it still works but it redirects to /page.
The htacces file should look like this:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
You redirect any php file in the url with the same name withouth the "php".
Then in your php files, you can check to see if the url contains the extension (http://blabla.com/bla.php) and redirect the page to the same one withouth the extension.
So, at the beginning of each php file you should call this function :
function redirectIfNeeded(){
$url = $_SERVER["REQUEST_URI"];
if(preg_match("/\.php/$", $url))
header("Location: ".preg_replace("/\.php/",$url));
}
Please crate a .htaccess file in root directory an use this code!!
Options +FollowSymlinks
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+)$ /$1.php [L,QSA]
Hope!! it would be useful for someone!
RewriteEngine on
Rewritecond %{REQUEST_URI} !-f Rewritecond %{REQUEST_URI} !-d Rewritecond %{REQUEST_URI} !-l RewriteRule ^([\w\d-]+)$ $1.php [L]
RewriteRule ^([^/.]+)$ $1.php [L]

Categories