I have apache setup to wildcard direct all unknown trafic to one php script (default documentroot). This php script looks into a database and possibly finds the correct documentroot for the requested domain (or documetroot of the 404 error handler etc..).
The problem is, that from within php script I know how to include another script, but can not change the documentroot - ie. force apache to set the found documentroot for the request, read htaccess file, run scripts etc.. can I? Any other solution? Must be allready somehow solved and solution is probably not very difficult, but was not able to create a fully working setup so far..:-( thanks
You can send the Location header to the browser to force it to redirect to another URL.
Maybe you should redirect this traffic wherever you want by using Location:
Header("Location: /this-new-page/");
You can check the php manual at the Header page.
Related
Suppose i have a link generated in php:
http://localhost/Example/www.someDomain.com
now i would like to go to www.someDomain.com , can you please help me with the proper method (probably via .htaccess) to redirect to desired location.
I tried using php header function and js window.location.replace(" ") but it dint' helped.
***Problem with window.location.replace("www.someDomain.com") is that instead of redirectng to www.someDomain.com , the page redirects to http://localhost/www.someDomain.com
I am using Wamp server if it helps.
Help please and also suggest some nice resource to study about .htaccess
Thanks
If I understood.
you need to put te entire URL with "http://".
Javascript:
<script>
window.location.href=("URL");
</script>
OR in php:
<?php
header("location:$redirect");
?>
You can use window.location or PHP header() function, but for redirect to another site must prefix with http://.
header("Location: http://www.example.com");
You can also do the same through Apache .htaccess with a Redirect rule.
htaccess is NOT going to help you at all. If all your links look like: http://localhost/... then in order for htaccess to redirect, everyone who goes to your site would have to be running a webserver on their local machine and have an htaccess file that redirects to your site.
You need to fix your php scripts so they generate the correct hostname. Using javascript to redirect isn't even a viable solution, because in order for the location to be "localhost", they're already loading no content because they're trying to connect to their own machine.
If for whatever crazy reason you can't fix your scripts, you may have to look into using mod_proxy_html on your site and dynamically change all your "localhost" to your domain.
I'm having a strange problem with a new PHP page I´m writing. Everything seems to be loading/redirected to the domain root page.
domain.com loads fine
domain.com/page or domain.com/page/index.php always loads "domain.com" while retaining the /page/index.php in the browser URL.
Even domain.com/some_random_text loads "domain.com"
I suspected the .htaccess file, but there is none to be found. There are no redirects in the PHP code.
If I remove the domain.com/index.php file I get an Internal Server Error when opening domain.com/page/index.php
This domain did have Wordpress installed, but I did uninstall that via cPanel.
First thing to notice is that the .htaccess file can be anywhere in the path that is handled by Apache. So check everywhere. This kind of redirect could be made only like that.
This can only be done by Apache, so you might also like to check the apache config if you are running on a virtual machine or dedicated server.
Also, not that it might have been a wildcard redirect, added by the cPanel, so check those settings. Though I think those actually just modify the root .htaccess file.
Based on the data you provided I cannot say more, but I don't think this is a common issue.
I have a php website. There is a domain example http://www.mydomain.com/clubs.php I want to mast it to http://www.mydomain.com/groups.php and the rest of the address will remain the same. Is it possible.
Please guide me how to achive it..
You could create a new file named groups.php and include in it clubs.php.
Or you could use mod_rewrite via .htaccess file.
For including file use:
<?php include_once 'clubs.php'; ?>
For rewriting use and add it after line RewriteEngine On:
RewriteRule ^groups.php$ clubs.php [L]
Hope I've understood what you're talking about with mask.
If I understood you correctly, then you need to use apache directive URLRewrite (mod_rewrite). Google it. It's complex to explain here, but basicaly there are few lines to add in .htaccess file and your webserver must support it. Some do not supoprt for securty reasons. If the server is administered by you then you can easily modify this in VirtualHost directive of yout httpd.conf file.
maxim
Use the Apache mod_rewrite module, see below :
http://httpd.apache.org/docs/current/mod/mod_rewrite.html
http://net.tutsplus.com/tutorials/other/using-htaccess-files-for-pretty-urls/
http://docforge.com/wiki/Clean_URL
You cannot "mask" a domain or URL. A link points to a certain URL. That URL is necessary for the browser to find the server it's supposed to send the request to and to make the request. The browser will send the request to that URL and it will display the URL it sent the request to in the address bar. You cannot make it send a request to one URL but have it display a different URL.
You can make your webserver react to that URL/request any way you want. Just because the request said "clubs.php" doesn't mean the webserver will execute a file called "clubs.php" or that it has to execute some PHP file at all. That can be customized in the webserver itself, typically through URL rewriting in Apache. That's all just internal to the webserver however, it configures how your webserver reacts to an incoming request for a certain URL; it does not "mask" the URL.
If you have links pointing to "clubs.php" but you want them to point to "groups.php" instead, you'll have to change your links. No way around it.
You could redirect all requests for "clubs.php" to "groups.php". I.e., when your webserver receives a request for "clubs.php", it responds to the client by telling it to ask for "groups.php" instead. That's a redirection. It will still make your links point to "clubs.php" though.
Currently, my page URLs look this this:
http://ourdomain.com/articles/?permalink=blah-blah-blah
I want to convert these to:
http://ourdomain.com/articles/blah-blah-blah
How can I accomplish this using PHP but not with .htaccess?
How can i accomplish this using php but not with .htaccess..
You can't. You will need to tell the web server how to deal with URLs that don't physically exist. In Apache, that is done in the central configuration or in a .htaccess file.
If your server already happens to have AccepPathInfo On, you can try having URLs like
http://ourdomain.com/index.php/articles/blah-blah-blah
which will redirect to index.php and have articles/blah-blah-blah in the $_SERVER["PATH_INFO"] variable. This method is known as "poor man's URL rewriting" because you can't get rid of the index.php part in the URL. If the mentioned setting is turned on (I think it is by default), you may be able to do this without using a .htaccess file.
You can achieve this without mod_rewrite if you have access to the server configuration. Assuming you're using Apache, the first thing you would need to do is turn the MultiViews option on on your document root (ie. add Options MultiViews). Now copy your /articles/index.php to /articles.php (so put the script in your document root and rename it), and adapt your script so it reads $_SERVER["PATH_INFO"] to fetch the correct page (this of course relies on having AcceptPathInfo On).
MultiViews will make sure that the articles.php script is called when you provide a /articles/blah-blah URL.
I don't think you can easily do it without altering .htaccess. You'll most definitely need to use mod_rewrite. See the answers here for more info:
Special profile page link like www.domain.com/username
It is possible to do it in PHP, without modifying .htaccess
Just write following code in either index.php or default.php
<?php
if (isset($_GET['permalink'])) {
header('Location: '.urlencode($_GET['permalink']));
}
?>
It works because when you type following URL:
http://ourdomain.com/articles/?permalink=blah-blah-blah
The filename is not specified.
So, the server looks whether "index" or "default" file is present in the specified directory.
Consider file index.php is present, so server will call:
http://ourdomain.com/articles/index.php
with blah-blah-blah in GET variable permalink
The PHP code checks if permalink GET variable is present, and redirects using header() method.
EDIT: added urlencode() to do input validation
I'm writting a HTTP server and when i tried the Serendipity PHP Blog i get requests from the browser like this one:
GET /serendipity_admin.php/templates/default/admin/pluginmanager.css HTTP/1.1
The "/serendipity_admin.php" file does exist so the
"/serendipity_admin.php/templates/default/admin/pluginmanager.css" obviously must fail.
What should a webserver do when it discounters such a request?
I tried to just execute "/serendipity_admin.php" with the usual HTTP request headers (REQUEST_URI, PATH_INFO etc), but this does not seem to work so there is some more magick required. Can someone please explain.
the web server will try to match the url from left to right.
since it finds the .php file, it will call it , the rest of the url will then be stored in the $_SERVER['PATH_INFO'] global PHP variable that is available for reading by the script.
Then the script can perform its logic depending on this variable.
REQUEST_URI and PATH_INFO are not HTTP request headers. They are CGI environment variables.
A request for /serendipity_admin.php/templates/default/admin/pluginmanager.css results in /serendipity_admin.php being run with PATH_INFO as /templates/default/admin/pluginmanager.css
You should read the CGI specification.
The blog probably excepts that you are running a module that works like Apache's MultiView. Multiview causes Apache to look for serendipity_admin.php (among other things) if it cannot find the file with the exact path specified in the query.
This looks like it's expecting you to use Apache's rewrite engine. Did it come with a .htaccess file? If so you'll need to set AllowOverride All in the VirtualHost or copy the contents of the file into the virtual host.