How I can do to remove .php extension (and html if it's possible in all link) without changing all links ?
I have search and I have found this solution :
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
RewriteRule ^([^\.]+)$ $1.html [NC,L]
It's work good but there are a problem, in fact in my header (or all post form), my link looks like this :
Action
So then users click on, the url is :
mysite.com/action.php
How I can do to all my url don't have .php exentison without changing all links in my website
Thank you
This will Work
RewriteEngine on
RewriteRule ^(.+)\.php$ /$1 [R,L]
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*?)/?$ /$1.php [NC,END]
On apache 2.4 and later, END flag is used to prevent infinite loop error.
Related
I have modified my .htaccess file right now to remove the .php extension to my website. I also have a rule to have a pretty url. My problem however is I'm not sure how to add a second parameter. In addition I wanted to know if it was a problem for the rules if these parameter aren't set.
For example as of now my rules will change mywebsite.com/index.php to mywebsite.com and mywebsite.com/login.php to mywebsite.com/login and mywebsite.com/index.php?week=1 to mywebsite.com/1.
However I have now added a new parameter and my url will now be mywebsite.com/index.php?page=a?week=1 and I would like it to be change to mywebsite.com/a/1 However the "week" or the "page" parameter arent always going to be set. So it could sometimes just be index.php?week=1 or index.php?page=a or they could both be set.
Here are my current rules:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)$ $1.php [NC,L]
RewriteRule ^([0-9]+)$ index.php?week=$1
RewriteRule ^([0-9]+)/$ index.php?week=$1
Is what Im asking for even possible?
You can also try this
RewriteEngine on
For week :
RewriteRule ^index/(\d+)$ index.php?week=$1 [NC,L]
For page:
RewriteRule ^index/(\w+)$ index.php?page=$1 [NC,L]
For both week and page:
RewriteRule ^index/(\d+)/(\w+)$ index.php?week=$1&page=$2 [NC,L]
From your answer to my question (see comments), page is letters only while week is digits only, which makes them distinguishable. As a consequence, you do not have to modify your URL scheme by adding an upper-level to distinguish both cases.
That said, a working solution for your problem would be:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)$ /$1.php [L]
# /digit/ for "week" only
RewriteRule ^([0-9]+)/?$ /index.php?week=$1 [L]
# /alpha/ for "page" only
RewriteRule ^([a-z]+)/?$ /index.php?page=$1 [NC,L]
# mix of both, order is /page/week/
RewriteRule ^([a-z]+)/([0-9]+)/?$ /index.php?page=$1&week=$2 [NC,L]
I have a website with a menu. When clicking on any of the pages in the menu it should go to the particular page whether it is page.php or page.html but it should not show the .php and .html in the url. How can I achieve this?
I have written a piece of code in htaccess. But it is not working.
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^(.*)$ $1.html
This isn't really a php question, but rather an apache question.
You have the right idea in that you need to use the .htaccess file for this.
However, don't forget to make sure that the RewriteEngine is set to ON, and to have the parameters at the end inside [ ] brackets.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
You can read more about this here
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 am attempting to remove .html and .php extensions from my website's URLs. I have been suggested to use the following code:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.html [NC,L]
This indeed works, however it will only work for one extension at a time. So I can either have .html removed, or .php removed from the URL. I need to have both removed simultaneously. Someone suggested I just rename all my .html files to .php, however this will not work for me. Previously, I was able to remove both extensions purely using .htaccess, but am unable to find the solution that I used before.
I also tried this code and it only removes the extension for .html:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.html [NC,L]
RewriteRule ^([^\.]+)$ $1.php [NC,L]
What code do I need to make this work?
Thank you in advance for your assistance!
if php files have more prioritet, than html ones, swap groups of lines
RewriteEngine On
# if exist file.html
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^([^\.]+)$ $1.html [NC,L]
# if exist file.php
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
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/