I never really have used mod re write and I am trying to understand the best way to implement it. removing the .php extension. i found this code
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
problem is that if the link is to domain.com/index.php it wont re write it domain.com/index
Also if i do the link domain.com/index the person can still add the .php and it still works fine.
I would like to know the proper way.. do i have to hard code the urls in the html or is there something that can do it automatically?
I dont want them to be able to add the .php to the end
You need an additional rule to remove .php . The rule you are using just allows you to access php files without their extension but doesn't remove the extension. To remove the .php you can use :
RewriteEngine on
#1) redirect "/file.php" to "/file"
RewriteCond %{THE_REQUEST} /([^.]+)\.php [NC]
RewriteRule ^ /%1 [L,R,NE]
#2) internally map "/file" back to "/file.php"
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
Related
I am not a professional but with help from SO I was able to rename the profile page URL's for users on my project using Rewrite_mod using:
RewriteEngine On
RewriteRule ^([a-zA-Z0-9_-]+)/$ profile.php?username=$1 [QSA]
This works good. But I can't figure out how to give a specific name to a particular page. For example I have a page profile_settings.php when users go to that page I want it to be example.com/settings in the address bar. Tried few ways from SO like removing .php thought it will remove php extension for all files but It did not work at all. Now my .htaccess looks like:
RewriteEngine On
RewriteRule ^([a-zA-Z0-9_-]+)$ profile.php?username=$1 [QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
Was hoping example.com/profile_settings.php to be example.com/profile_settings but it did not work it's still full address with file extension. Moreover It will be good if I can deal with single file separately as besides removing extension I also want it to give desired name.
Rewrites for specific cases like /settings -> /profile_settings.php need to be handled separately and before other generic rules like this:
RewriteEngine On
# specific rewrite rules
RewriteCond %{THE_REQUEST} /profile_settings\.php[\s/?] [NC]
RewriteRule ^ /settings [R=301,L]
RewriteRule ^settings/?$ profile_settings.php [L,NC]
# add .php extension if corresponding .php file exists
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+?)/?$ $1.php [L]
# handle profile page
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([\w-]+)/?$ profile.php?username=$1 [QSA,L]
I'm using the below code to remove .html extention form url, but it is not working..
can any one help , where i have to put the .htaccess file
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^([^/]+)/$ $1.html
I would like to share an understandable, simple way to remove the .html extension from the URL using the .htaccess file. I'm using XAMPP on Windows 10. For me, it's working perfectly. If you want the same result, just follow the steps given below...
For removing the .html extension, e.g. if you want to change mywebsite.com/contact.html to mywebsite.com/contact you have to add the following code inside your .htaccess file:
First of all, create a new file named .htaccess and placed it at your root-directory where your index file is placed.
Now, open this (.htaccess) file with any editor of your choice and write/copy/paste the given code in your file and save it...
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^.]+)$ $1.html [NC,L]
Now, if you (any user) will access mywebsite.com/contact in the browser, it will show the content of mywebsite.com/contact.html page.
So, what if any user will access the URL as mywebsite.com/contact.html? this will redirect the user to this mywebsite.com/contact.html page instead of this mywebsite.com/contact which means the user can still visit the mywebsite.com/contact.html page. Don't worry about it. If you want to avoid this, the next step is for you...
To avoid the above problem and for redirecting users to the newly created URL, you need to add some more rules in your .htaccess file. So, open it again and replace your old code with the new one given below and save your file...(You can ignore this step, It's on you...)
RewriteEngine On
RewriteCond %{THE_REQUEST} /([^.]+)\.html [NC]
RewriteRule ^ /%1 [NC,L,R]
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^ %{REQUEST_URI}.html [NC,L]
Now, you're all set.
I hope this will fix everyone's problem.
Have a nice day :)
You can use this code in your DOCUMENT_ROOT/.htaccess file:
RewriteEngine On
RewriteBase /
RewriteCond %{THE_REQUEST} \s/+(.+?)\.html[\s?] [NC]
RewriteRule ^ /%1 [R,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^(.+?)/?$ $1.html [L]
Finally I Got the correct .HTACCESS code to remove .php extention... :)
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
## hide .php extension
# To externally redirect /dir/foo.php to /dir/foo
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R,L,NC]
## To internally redirect /dir/foo to /dir/foo.php
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^ %{REQUEST_URI}.php [L]
Use this
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^(.*)$ $1.html [NC,L]
I'm trying to get the following functionality to work in PHP without a framework. I don't want to have to worry about setting up a super complicated framework for every PHP application I do.
http://domain.com/sign_up.php
becomes
http://domain.com/sign_up/
http://domain.com/user.php?id=432
becomes
http://domain.com/user/?id=432
Or if there is a way to get that to become http://domain.com/user/432 but i'm not sure how to handle multiple $_GET variables in that scenario so that's optional.
This works pretty well so far:
RewriteRule ^sign_up/([^/]*)$ /sign_up.php?p=$1 [L]
The only problem is I have to do that for every single php file i'm using which can become a lot.
What is a universal way to do it for all php files?
UPDATES:
This one line is working perfectly:
RewriteRule ^(.*)\/$ $1.php [NC]
Only issue is it doesn't auto redirect PHP
For example, I want to 301 auto redirect:
http://domain.com/file.php
to
http://domain.com/file/
And
http://domain.com/file.php?var1=value&var2=value
to
http://domain.com/file/?var1=value&var2=value
If anyone can think of a better way to handle query string values in a more SEO friendly way that would be awesome! But otherwise this is working pretty great so far.
MORE UPDATES:
Now this is working:
http://domain.com/file/ - to -
http://domain.com/file.php
Both of those point to the same page with this htaccess code:
RewriteRule ^(.*)\/$ $1.php [NC]
However http://domain.com/file without the trailing / returns a page not found error.
Also I need to know how to auto redirect http://domain.com/file.php to http://domain.com/file/
MOSTLY WORKING HTACCESS
This .htaccess works beautifully:
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !\.(php|html?|jpg|gif)$
RewriteRule ^(.*)([^/])$ http://%{HTTP_HOST}/$1$2/ [L,R=301]
RewriteRule ^(.*)\/$ $1.php [NC]
The only thing it doesn't do is auto redirect if they go directly to http://domain.com/file.php it does not redirect to http://domain.com/file/ but everything else about it is working.
Try this:
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d # not a dir
RewriteCond %{REQUEST_FILENAME} !-f # not a file
RewriteCond %{DOCUMENT_ROOT}/$1.php -f # but php exists
RewriteRule ^([^/]+)/([^/]+)?$ $1.php?p=$2 [L]
However http://domain.com/file without the trailing / returns a page not found error.
That's because your rule does not match unless there's a / at the end.
RewriteRule ^(.*)\/$ $1.php [NC]
^
You can make it optional with ? as
RewriteRule ^(.*?)/?$ $1.php [L]
Note, that / does not need a \ before it. It works with or without it.
Also I need to know how to auto redirect http://domain.com/file.php to http://domain.com/file/
# Rewrite original .php request to new URLs
RewriteCond %{THE_REQUEST} \ /([^.]+)\.php [NC]
RewriteRule ^ /%1/ [R,L]
# Resolve the new URLs to .php files
RewriteRule ^(.*?)/?$ $1.php [L]
If you get this working first, we can see what we can do about the query parameters later.
Your final htaccess could look like
# Rewrite original .php request to new URLs
RewriteCond %{THE_REQUEST} \ /([^.]+)\.php [NC]
RewriteRule ^ /%1/ [R,L]
# Force a trailing / if not a file
RewriteCond %{REQUEST_URI} !\..{3,4}$
RewriteRule ^(.*)([^/])$ http://%{HTTP_HOST}/$1$2/ [L,R=301]
# Redirect to php if not an existing dir
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ $1.php [L]
You'll probably want something like this:
RewriteEngine on
RewriteBase /
# redirect with trailing parameter
RewriteRule ^([\w]+).php?p=([\w]+)$ $1/$2/ [QSA,R=301]
# redirect bare php files
RewriteRule ^([\w]+).php$ $1/ [QSA,R=301]
# make sure it's not a request to an existing file
RewriteCond %{REQUEST_FILENAME} !-f
# make sure we have a trailing slash
RewriteRule ^(.+[^/])$ $1/ [QSA,R=301]
# internally point to the right file
RewriteRule ^([^/]+)/([^/]*)/?$ $1.php?p=$2 [QSA,L]
The [R=301] appendixes redirect the browser to the new URL with a 301, moved permanently status header. That way the browser will know where to find the right url in the future, without asking the server.
Also, sometimes an .htaccess checker is useful: http://htaccess.madewithlove.be/ Do note, the tool doesn't work with %{REQUEST_FILENAME}.
I just need to know how can I stop displaying my php pages name in url
for example:
http://cbse.in/classxii/question_papers.php to http://cbse.in/classxii/ or http://cbse.in/classxii/question_papers/
http://cbse.in/classxii/computer_science/question_papers.php to http://cbse.in/classxii/computer_science/ or http://cbse.in/classxii/computer_science/question_papers/
http://cbse.in/classxii/computer_science/Chapter1/answers.php to http://cbse.in/classxii/computer_science/Chapter1/ or http://cbse.in/classxii/computer_science/Chapter1/answers/
I came to know this is possible by URL rewriting in htaccess.
But not clear idea on this, it will great if someone show me the correct and clear way to do this.
Please refer this link for info about htaccess redirection. Also, pls make sure that you've got it enabled within Apache, the webserver.
http://www.addedbytes.com/articles/for-beginners/url-rewriting-for-beginners/
try this:
RewriteEngine on
#unless directory, remove trailing slash
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ $1 [R=301,L]
#redirect external .php requests to extensionless url
RewriteCond %{THE_REQUEST} ^(.*)\.php([#?][^\ ]*)?\ HTTP/
RewriteRule ^(.*)\.php$ $1 [R=301,L]
#resolve .php file for extensionless php urls
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php [L]
You need to use Mod Rewrite. You can use this one Mod Rewrite generator: http://www.generateit.net/mod-rewrite/
I want to remove the extension of file which is in subdirectory i.e. abc.com/subdirectory/file.html, tried evry possible way with rewrite rule, but nothing seems to work.
I think you want something like
http://www.abc.com/subdir/file right?
So you can do it with .htaccess with
## Remove Extension ##
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^(.*)$ $1.html
I use it with php and then I simply write www.abc.com/subdire/file
*REMEBER * I don't know if this solution suit all your needs, your question is really general. Please provide some information
This code will work for you.
URL 'domain.com/index.html' to display as 'domain.com/index'
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^(.*)$ $1.html