apache htaccess Header Set only working for static files - php

I have a php application that directs all urls that don't point to a static file to index.php (configured in .htaccess)
I added the following configuration to add a header to all requests in my .htaccess file, but the header is only applied to urls that go to static files.
Header set Access-Control-Allow-Origin *
What could prevent the header from being applied to index.php?

So the problem was that the mod_header module wasn't working with php fastcgi. It started working after switching to mod_php.

Related

Redirecting Urls via PHP without htaccess

I want to redirect my urls but not with htaccess, only with php. I know that I have to use the header() function. But my question is how to catch the url.
For example, Wordpress catches urls like mysite.com/postname and redirects it to other urls, I think it is index.php?parameters=values.
But my question how to catch the url mysite.com/postname and redirect it to other. Which php script will catch it.
Or when wordpress catch the url, which php file redirects it to index.php
You need to tell your HTTP server that the URL is handled by the PHP script.
You can't do this with PHP directly: If the server never runs the PHP script, then the PHP script can't do anything with the request!
This is most commonly done with mod_rewrite which is configured using Apache configuration. There are two basic places that you can put mod_rewrite directives.
The main Apache configuration files
A .htaccess file.
The former is recommended:
You should avoid using .htaccess files completely if you have access to httpd main server config file. Using .htaccess files slows down your Apache http server. Any directive that you can include in a .htaccess file is better set in a Directory block, as it will have the same effect with better performance.
… and you've rejected .htaccess so put the directives in your main configuration file.
This, of course, assumes you are using Apache HTTPD. You didn't say which HTTP server you were using.
This also assumes that when you rejected .htaccess you didn't mean mod_rewrite. Many people conflate the two since changes to mod_rewrite settings are the most common things that programmers (as opposed to System Administrators) want to do in an Apache configuration.
For example, Wordpress catches urls like mysite.com/postname and redirects it to other urls
Wordpress uses mod_rewrite directives in a .htaccess file.

Running php file by avoiding extension .php in the url without using .htaccess

In my Localhost:
Without using .htaccess, I created a folder named test and a file test.php.
I can run the file from url http://localhost/test/test
In my amazon server with cpanel:
I put the same folder with that file but displays internal server error after checking the url http://example.com/test/test
I tried:
Changing the Allowoverride None to All
enabling mod rewrite
This is actually for a large project. But using a test in this case.
It is possible to rewrite URLs without an htaccess file. That's because you can also use rewrite rules in the Apache configuration files. In a VirtualHost block for example. I guess your localhost is set up that way, so take a look in those server config files.
For your amazon server to work, you either can work with an htaccess file (easiest way) or you can put those rules in the server config files.

Apache does not generate a 404

if I have /faq.php on the server it can also be accessed via /faq.php/nonexistant.gif why? I have made sure MultiViews are disabled. Why does the contents of /faq.php get shown when I access the URI /faq.php/randomstuff.gif? FYI, I have no htaccess file in the same directory.
/nonexistant.gif will be HTTP "PATH_INFO": http://www.ietf.org/rfc/rfc3875, section 4.1.5
Basically, the webserver will scan "down" a url until it hits an actual file. Anything after that file in the url becomes PATH_INFO.
http://example.com/some/path/leading/to/realfile.php/extra/stuff/that/becomes/path/info
^^^^^^^^^^^^^^^^^^^^--- real directories
^^^^^^^^^^^^--actual file, scanning stops here
^^-----onwards = path_info
That is called path_info. You can disable it using AcceptPathInfo Off in the apache config. People generally use it as a fake mod rewrite when mod rewrite is not availalble.
http://httpd.apache.org/docs/2.2/mod/core.html#acceptpathinfo

how to move a apache localhost website to internet (i.e remote server)

i have few conceptual queries about moving a locahost site to server for making it available on internet.....what are the steps...how server will recognize the changes in my apache and php config files..i have built a website in php and mysql on apache 2.2
NOTES:
i have made some changes in my apache config files.one of them is i
have included a fillename "index.php" in
"DirectoryIndex"...so now my DirectoryIndex looks like:
<IfModule dir_module>
DirectoryIndex index.html index.php
</IfModule>
so now automatically my apache tells the browser to search for a index.php file whenever header('LOCATION:') or refresh is called.and it's obvious that my index.php files holdas the html and and other php codes which will be shown or displayed
third and most importantly...i have used header function in many cases while building the website..my header functions are like header('Location: http://localhost/home/');
so repeating the question again
should i change all the header functions while uploading acording to my website name
i.e
header('Location: http://www.my_site_name.com/home/'); in place of
header('Location: http://localhost/home/');
and who will my remote server will recognize the changes i have made in my config file and will work according to the changes
and what are the steps to upload a website to internet
and who will my remote server will recognize the changes i have made
in my config file and will work according to the changes
Normally hosting servers are configured to allow the use of .htaccess files to override apache configuration
you may create a .htaccess file in your root directory and place there your DirectoryIndex settings
should i change all the header functions while uploading acording to
my website name i.e header('Location: http://www.my_site_name.com/home/');
in place of header('Location: http://localhost/home/');
Why not using relative paths or simply:
header('Location: /home/');
and who will my remote server will recognize the changes i have made
in my config file and will work according to the changes
That way, it'll work on both servers.
and what are the steps to upload a website to internet
You should use a FTP client to upload your website to your webhost.

htaccess downloading file instead of loading

I would really like my index.html to be able to have a PHP script work on it. I read that you can do this through the htaccess file. I only have access to a subdomain website directory, where I can upload my files through FTP.
The directory did not have a htaccess file, so I created one using notepad: .htaccess and added this to the file:
AddType application/x-httpd-php .html
The problem is, instead of loading the index.html page, it downloads it as a file...would I need to add something extra to the htaccess file? :S
You don't need to name the file index.html to have it served by default. You can change the default document using your with an entry in your .htaccess file like this:
DirectoryIndex index.php
Then when you navigate to http://yoursubdomain.example.com you will be served index.php instead of index.html.
If really do want PHP to interpret your .html documents then the entry you had in your question will work when PHP is running as an Apache module. If your host is running PHP as CGI, you want:
AddHandler application/x-httpd-php .html
If it still doesn't work, then this web page has some more suggestions:
http://www.velvetblues.com/web-development-blog/how-to-parse-html-files-as-php/
The directive you have sets the content-type of files with a .html file extension.
If the server has PHP installed and enabled, that content-type will cause it to be run though the PHP engine and then the output from that sent to the client.
If it doesn't have PHP installed, then the file will just be served up to the client with that content-type. Since browsers don't handle PHP scripts themselves, they will then just save the file.
You need to install and enable PHP as well as setting the content-type.
Presumably your hosting is supporting PHP?
If so, then you need to rename your file from index.html to index.php

Categories