This is my question:
I want to remove the ".php" extension from my URL.
I found this code:
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /
RewriteRule ^forums/ - [L,NC]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*?)/?$ $1.php [L]
... which works, but I have a problem.
I don't really know why, but this code somehow changes the server request method from
POST to GET. So if I use this code in my .htaccess file, I can't use the POST method anymore.
So.. Is there any other way to hide the ".php" extension?
Thank you anyways :)
It works this way because you are [R]edirecting all requests ending in .php to a new URL without .php. You are doing a redirect. You cannot redirect POST requests. If you don't want the .php at the end, don't link to those URLs. Make your form action submit to the non-.php URL directly.
Related
What do i want to do is if the url have www.example.com/index.php/anything then it should throw the user to www.example.com/error-404
Here is my current expressions in my htaccess file.
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?$1 [L,QSA]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s(.*)/index\.php [NC]
RewriteRule ^ %1 [R=301,L]
RewriteCond %{HTTP_HOST} !^www.example.com$ [NC]
RewriteRule (.*)$ http://www.example.com/$1 [L,R=301]
Redirect 301 /online-features/key-anywhere-web-based-system http://www.example.com/online-features
Redirect 301 /home http://www.example.com/
Thanks in advance.
Please suggest/edit my question if i have not asked the question in correct way.
EDIT
I don't want to loose the /index.php to / redirection.
www.example.com/index.php/anything
/anything here is additional path information (after the name of a physical file). You can use the AcceptPathInfo directive to specifically disable this "feature":
AcceptPathInfo Off
In which case all URLs containing additional path info will automatically trigger a 404. However, the URLs can still be routed using mod_rewrite, which is probably what's happening here.
You will still need to implement a mod_rewrite redirect as mentioned in the datascript's answer. Or try something like the following, before your existing directives:
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^index\.php/ /error-404 [R=302,L]
The check against REDIRECT_STATUS is to avoid a rewrite loop, since your front controller appears to use path info to route the request.
Change this to a 301 if this is intended to be permanent, once you have confirmed it's working OK.
However, it would be preferable if this was implemented as a proper Apache error document. For example:
ErrorDocument /error-404.php
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^index\.php/ - [R=404,L]
I think the following should do what you require. The dot is any character and the + means one or more times.
This would require the forward slash to be there
RewriteRule ^index\.php/(.+)$ error-404 [L]
EDIT: Thanks #DocRoot, updated accordingly
I'am quite new with htaccess. I have successfully created .htaccess file with following rules to redirect user with pretty url.
My actual url
http://localhost/domain.com/Job-Details.php?Job_ID=30
What i want to achieve
http://localhost/domain.com/30
My Current htaccess
RewriteEngine On
RewriteBase /domain.com/
# Get the URI-path directly from THE_REQUEST variable
RewriteCond %{THE_REQUEST} ^(GET|HEAD)\s/(.*)\.php [NC]
# Search friendly URLs for job detail page
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([0-9]+)$ Job-Details.php?Job_ID=$1 [NC,L,R=301]
Issues I'am facing
Redirection with pretty url not happening. I'am stil seeing old url
Using mentioned htaccess when i enter pretty url in address bar directly then page displays fine but url gets changed to my current url automatically.
any idea what wrong i'am doing here.
Please try with the following /domain.com/.htaccess code:
RewriteEngine On
RewriteBase /domain.com/
# Redirect actual URI to SEF URI
RewriteCond %{THE_REQUEST} (?:GET|HEAD)\s\/domain.com\/Job\-Details\.php\?Job_ID\=(\d+) [NC]
RewriteRule ^ %1? [R=302,L,NE]
# SEF URI for Job-Details page
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(\d+)$ Job-Details.php?Job_ID=$1 [NC,QSA,L]
Change R=302 to R=301 if you are happy with the results. Remember to clear your cache before testing this, otherwise your browser may redirect based on historical cache.
Change this:
RewriteRule ^([0-9]+)$ Job-Details.php?Job_ID=$1 [NC,L,R=301]
RewriteRule ^([0-9]+)$ Job-Details.php?Job_ID=$1 [L,QSA]
With R=301 you were creating a 301 redirection.
i am able to remove .php extension from url but i want if user enter the url with .php extension then page should show the 404 error using .htaccess
Any Help Please
You can put this code in your DOCUMENT_ROOT/.htaccess file to hide .php extension:
RewriteEngine On
RewriteBase /
# To externally redirect /file.php to /file
RewriteCond %{THE_REQUEST} \s/+(?:index)?(.*?)\.php[\s?] [NC]
RewriteRule ^ %1 [R=301,L,NE]
# To internally forward /file to /file.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1\.php -f [NC]
RewriteRule ^(.+?)/?$ $1.php [L]
You are trying to make a clean url i think.
Here is a link that describes things to do.
http://www.desiquintans.com/cleanurls
I hope this helps.
I have read quite many responses about htaccess but it is still so confusing to me. I have the following rule in my .htaccess file, basically removing the .php extension from the files and resolving the extensionless URL's:
RewriteEngine On
# Unless directory, remove trailing slash
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/$ http://www.example.com/$1 [R=301,L]
# Redirect external .php requests to extensionless url
RewriteCond %{THE_REQUEST} ^(.+)\.php([#?][^\ ]*)?\ HTTP/
RewriteRule ^(.+)\.php$ http://www.example.com/$1 [R=301,L]
# Resolve .php file for extensionless php urls
RewriteRule ^([^/.]+)$ $1.php [L]
However, when I want to contact e.g. contact_form.php (that handles processing of the form data from a form in HTML), it is being rewritten to contact_form and all the POST data is lost. I would like to achieve that when the request contains post data, the URL should not be redirected/rewritten. I have honestly no idea how the rewrite rule should look like. All help greatly appreciated!
You can insert this rule as first rule to ignore POST requests:
RewriteCond %{REQUEST_METHOD} POST
RewriteRule ^ - [L]
I have a rewrite condition in my .htaccess file which removes the need for .php file extension
RewriteEngine On
RewriteRule ^([^.]+)$ $1.php
so http://site.com/blog opens http://site.com/blog.php
but if old users type /blog.php it will also load the page
is there a way to prevent or redirect pages with .php or any other file extention to the one without it?
i mean if user entered /blog.php or /blog.asp it should either fail to load or redirect to /blog (without extention)
A better way to accomplish this would be to only rewrite if a .php by that name exists. Otherwise throw 404 for the original URL. The second set of rules would take care of removing the extension and avoiding the redirect loop.
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php [L]
RewriteCond %{THE_REQUEST} ^(?:GET|POST)\ /.*\.php\ HTTP.*$ [NC]
RewriteRule ^(.*)\.php$ $1 [R=301,L]
RewriteEngine On
RewriteRule ^([^.]+)$ $1.php [L]
RewriteRule ^(.*?)\.php$ $1 [R=301,L]
You can use the rule
RewriteRule ^(.+)\.php$ $1 [R=301,L]
This would cause apache to send back a redirect to the browser which would update it's URL to the one stripped from the extension. But make sure to place this rule in front of the one the redirects to .php internally
Try this:
RewriteRule ^(.*?)\.([a-z0-9]+)$ $1 [R=301,L]