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]
Related
Recently I was thinking of making some seo friendly urls by rewrite and redirecting urls.
the rule which I wrote is working fine for this type of url like xyz.com/abc.php this automatically got converted into xyz.com/abc now I need help with this type of urls abc.com/study.php/abc/123 which I want to redirect on abc.com/study/abc/123
my .htaccess file is
RewriteEngine On
# browser requests PHP
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^\ ]+)\.php
RewriteRule ^/?(.*)\.php$ /$1 [L,R=301]
# check to see if the request is for a PHP file:
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^/?(.*)$ /$1.php [L]
EDIT.
It was working fine now I have changed my mind that instead of removing .php extension from all files I want to remove .php extension specifically from 2 files i.e a.php and b.php.
You can tweak your existing rules to support this PATH_INFO as well like this:
# browser requests PHP
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\s/+(\S+?)\.php(/\S*)?\sHTTP [NC]
RewriteRule ^ /%1%2 [L,R=301,NE]
# check to see if the request is for a PHP file:
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^/?(.+?)(/.*)?$ /$1.php$2 [L]
my url is like below :
http://localhost/project/test/jobs/?page=accounting
Accounting
And i want to make like below :
http://localhost/project/test/jobs/accounting
i have search other question but unable to make it. As i am totally unaware about this .htaccess rewrite process :(
however i have below code already in my htaccess file to remove .php extension :
RewriteEngine On
# Unless directory, remove trailing slash
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/$ http://example.com/folder/$1 [R=301,L]
# Redirect external .php requests to extensionless url
RewriteCond %{THE_REQUEST} ^(.+)\.php([#?][^\ ]*)?\ HTTP/
RewriteRule ^(.+)\.php$ http://example.com/folder/$1 [R=301,L]
# Resolve .php file for extensionless php urls
RewriteRule ^([^/.]+)$ $1.php [L]
please help me to append URL rewrite rule in above htaccess file without loosing .php ext rule.
you are making get request with params which remains as it is if u wana make post request then it will be somethong like this
http://localhost/project/test/jobs/accounting
What I normally do with link building is sending all my traffic to my index.php whitch splits it into a MVC framework, this is the important part of my .htaccess for you:
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*) index.php?path=$1 [L,QSA]
What does this .htaccess do rule by rule?
Options +FollowSymLinks
http://www.maxi-pedia.com/FollowSymLinks
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
Checks or the requested file isn't a direct path to a directory or actual file, correct me if I am wrong, because I am not 100% sure.
RewriteRule (.*) index.php?path=$1 [L,QSA]
Rewrites the path from "/test/testings/lalalala" into "index.php?path=/test/testings/lalalala".
I hope that this was the answer where you were looking for.
I am using one htaccess code (Core PHP) to hide php extension from url and its working great but not true with second segment of url
for example if url is http://www.abc.com/somepage than my code is running great but if url is
http://www.abc.com/xyz/sompage than my code will fail.
here is my code of htaccess
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/$ http://www.abc.com/$1 [R=301,L]
# Redirect external .php requests to extensionless url
RewriteCond %{THE_REQUEST} ^(.+)\.php([#?][^\ ]*)?\ HTTP/
RewriteRule ^(.+)\.php$ http://www.abc.com/$1 [R=301,L]
# Resolve .php file for extensionless php urls
RewriteRule ^([^/.]+)$ $2.php [L]
Please help me to find out hte solution that no matter how much segments are there but it should take last segment as a php file
Thanks in advance
Try to add:
RewriteCond %{HTTP_HOST} !^www.abc.com/xyz [nc]
RewriteRule !^(.*)$ !http://www.abc.com%{REQUEST_URI} [R=301,L]
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.
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/