NOTE:this may be a POSSIBLE DUPLICATE question. But my .htaccess is a bit different and the problem is unable to post to next page....
ie:form action="another.php" method="post"> orform action="another" method="post"> is not working.
Can any one kindly tell me what is the change that has to be made
. my .htaccess page is shown below
RewriteEngine On
#submydomain and folders
RewriteCond %{HTTP_HOST} !^jobs\.mydomain.in [NC]
RewriteRule ^(.*)$ http://jobs.mydomain.in/$1 [R=301,L]
#remove .php and ad slash
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/$ http://mydomain.in/jobs/$1 [R=301,L]
# Redirect external .php requests to extensionless url
RewriteCond %{THE_REQUEST} ^(.+)\.php([#?][^\ ]*)?\ HTTP/
RewriteRule ^(.+)\.php$ http://mydomain.in/jobs/$1 [R=301,L]
# Resolve .php file for extensionless php urls
RewriteRule ^([^/.]+)$ $1.php [L]
If you're hiding .php extension then you shouldn't keep it in your form action. So have your <form> like this:
form action="another" method="post">
Otherwise your posted URL will be externally redirected to /another by removing .php extension and your POST data will be lost.
I deleted the entire folder and uploaded the folder without .htaccess..The form worked...Later I uploaded the .htaccess (with the code that I posted in the question) and now working fine..
BUT FOR THE ANSWER AS TO WHY IT DID NOT WORK INITIALLY ,I DONT KNOW...FOR ANY ONE WHO KNOW CAN KINDLY POST..(CURIOUS TO KNOW)
Any way thanks for all those who helped me
Related
This morning, I was trying to remove file extensions from a website I am working on. While I am not familiar with .htaccess files, I am trying to learn them and their regex online. The first one that I tried failed to preserve $_POST values, and therefore broke the website. I found this longer block of .htaccess code on here and tried using it, and it fixes the post issue well.
DirectoryIndex index.php
RewriteEngine On
RewriteBase /
# Unless directory, remove trailing slash
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/$ /$1 [R=301,L,NE]
# Redirect external .php requests to extensionless url
RewriteCond %{REQUEST_METHOD} !POST
RewriteCond %{THE_REQUEST} /.+?\.php [NC]
RewriteRule ^(.+?)\.php$ /$1 [R=301,L,NE]
# Resolve .php file for extensionless php urls
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1\.php -f [NC]
RewriteRule ^(.+?)/?$ $1.php [L]
However, I have found that while it drops extensions well, when I go to certain files or back from them, my files have their extensions back. This appears to stop when I destroy the session. I hope this does not seem like a trivial or repeated question, but I find it unusual that some files have their extensions hidden while others do not. For example, I will see Site/home, then go to Site/about.php, and when I come back I will be at Site/home.php. Is there something obvious I am missing here? Thank you!
RewriteEngine On
RewriteRule ^index?$ index.php
I figured this out. The .htaccess file works, for anyone trying to do the same thing. My forms were going to Site/about.php, which I assumed would automatically correct to Site/about. I went through my code and removed all file extensions from forms, hrefs were fine. This fixed it, so far no bugs! Thank you all who considered this question.
RewriteEngine On
RewriteRule ^about?$ Site/about.php
OR
RewriteRule ^Site/about?$ Site/about.php
I made a simple admin interface for one website and then I wanted to hide the .php from URL. But then the login stopped to work.
Login form (post method) is on page admin.php in root directory, after login it includes admin content from another file in protected directory.
What am I doing wrong?
.htaccess looks like this:
RewriteRule ^index\.php$ / [R=301,L]
RewriteRule ^(.*)/index\.php$ /$1/ [R=301,L]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /.*\.php\ HTTP/
RewriteRule ^(.*)\.php$ /$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+)$ $1.php [L,QSA]
EDIT: I noticed not only login form, but also all the ajax loaded modal dialogs aren´t sending data. Where I could, I removed redirections in forms and in ajax file I put url:file instead url:file.php where it was possible. And these corrections helped. But on some lines I need to have the extension written. So I can´t use this and still need some proper settings in .htaccess for these few cases.
You have made your .htaccess very complicated.
if only url-rewriting is your goal then you can do it simply by
*Replace YOUR_DIRECTORY by your folder name or remove it with slash.
RewriteEngine on
RewriteRule ^([a-zA-Z0-9-_]+)/?$ YOUR_DIRECTORY/$1.php [NC]
Note :- There is no need to forbid requests with .php extension, because you have
sad that these files exists in another protected directory, then it
will automatically throw 404 error.
I am using .htaccess file for url rewriting . I've written all pages with .php extention and to hide them i am using this code
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]`
But this hides when i write "www.example.com/abc.php" to "www.example.com/abc" . I want them to change automatically like when i click on a link
example
then page link should open like "www.example.com/example" not "www.example.com/example.php"
Also how to hide "?var="hello" from all pages but variable should be sent to that page is that possible ?.
UPDATE
For example here i am using this
<?php
if(isset($_GET['hi'])){
echo $_GET['hi'];
}
?>
a<br>
b<br>
c<br>
d<br>
I want what ever the page is should be accessed without extension and query string should be hided but must be accessed.
Use this .htaccess:
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]
For hidding ?val you should use POST instead of GET. And take a look on this article for .htaccess he has defined good in here.
Regarding hiding the query string. When the php page runs it can first check for a query string. If one is found you can pull the data out of the query string, store it somewhere (session probably) and redirect to the page with no query string (and no file extension for that matter).
However if you do this you're obscuring most of the benefits of using a query string.
To remove the php extension completly, you can use :
RewriteEngine on
#1redirect "/file.php" to "/file"
RewriteCond %{THE_REQUEST} /([^.]+)\.php [NC]
RewriteRule ^ %1 [NC,L,R]
#if the request is not for a dir
RewriteCond %{REQUEST_FILENAME} !-d
#and the request is an existing php file
RewriteCond %{REQUEST_FILENAME} !-f
#then rewrite the request to orignal php file
RewriteRule ^([^/]+)/?$ $1.php [NC,L]
I'm moving an old site from flat PHP files over to a new WordPress installation and want to make sure all the old URLs redirect properly. For example,
Old url: /va/apply.php
should now go to:
New url: /veterans-affairs/apply
I've got /va redirecting to /veterans-affairs properly, but cannot get the .php stripped from the URL.
I'm not sure if these needs to all be done in one step? I've tried everything I can find online and made as many tweaks as my limited knowledge in .htaccess has allowed.
This is also on WordPress, so there may be something I did that was conflicting with the pretty permalinks stuff there.
This is some of the code that I've tried among many others.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)$ $1.php [NC,L]
This should redirect the user to the non-PHP location, but I keep getting a 404. This must be a combination of my code and WordPress' pretty permalinks.
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.+)\.php$ $1 [L,QSA]
I have just had a quick look through where you are at and this above might help out. Add it to the wordpress htaccess above all the entries there so it can change this first... HTH
OK, I've finally got this working correctly. Again, what I'm trying to solve is to get this URL:
/va/apply.php
to correctly redirect to the new WordPress URL,
/veterans-affairs/apply
What worked for me was:
# This will remove the .php extension if it is not a directory, the file does not exist and it's not a WordPress specific admin page
RewriteCond %{REQUEST_URI} !/wp-(content|admin|includes)/ [NC]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php !-f
RewriteCond %{THE_REQUEST} ^(.+)\.php([#?][^\ ]*)?\ HTTP/
RewriteRule ^(.+)\.php$ $1 [R=301,L]
# The basic redirect for /va
Redirect /va /veterans-affairs
I think what was breaking it was this final line that you find in all the examples:
RewriteRule ^([^/.]+)$ $1.php [L]
I think this was trying to actually resolve the URL before WordPress could do what it needed to do.
I also found this page which proved insightful
Hide .php Extension, Set Directory Index, Eliminate Duplicate Content, etc.
I've been trying to hide the php extensions for my site. And I've been able to pull together a few Stackoverflow answers to do just that. However, I just have one last issue where when I POST, the resulting page is showing the .php extension. Can anybody help me on what I need to tweak with my htaccess file to not show .php completely?
# Turn mod_rewrite on
RewriteEngine On
RewriteCond %{REQUEST_METHOD} POST
RewriteRule ^ - [L]
# Unless directory, remove trailing slash
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/$ http://www.example.com/folder/$1 [R=301,L]
# Redirect external .php requests to extensionless url
RewriteCond %{THE_REQUEST} ^(.+)\.php([#?][^\ ]*)?\ HTTP/
RewriteRule ^(.+)\.php$ http://www.example.com/folder/$1 [R=301,L]
# Resolve .php file for extensionless php urls
RewriteRule ^([^/.]+)$ $1.php [L]
Here are the two resources I used from Stackoverflow:
How to hide .php extension in .htaccess
Website Form do not carry any value when .htaccess File Extension Remover Code is Used
Remove this:
RewriteCond %{REQUEST_METHOD} POST
RewriteRule ^ - [L]
It says:
Dear mod-rewrite
If the request method is POST, whatever the url is, would you please leave it
unchanged without doing any rewriting? And would you please not apply any other rules?
Sincerely yours,
etc. etc.