Current versions of MediaWiki have the front page accessible via
http://www.example.com/wiki/index.php/Main_Page
Instead of the older version of
http://www.example.com/wiki/index.php?title=Main_Page
That second URL is literally how the PHP script would be called in most casual web apps, but the above URL is cleaner and more desirable. Now, I can see how that could easily be converted with an Apache mod_rewrite rule:
RewriteRule ^/index.php/(.*)$ /index.php?title=$1
But there's no htaccess file in the default MediaWiki setup. So how are they doing the redirect?
Not sure if this is what they are doing, but there no need to redirect using a .htaccess. the page index.php is found and that script is what is loaded. index.php just gets $_SERVER['REQUEST_URI'] and parses that into the query string.
http://httpd.apache.org/docs/2.0/mod/core.html#acceptpathinfo
Related
i have a site abc.com in php and now made anothers site abcd.com on rails, i wish to redirect all requests that go to the php site to redirect to abcd.com...
example:
abc.com/page3 -> abcd.com/page3
abc.com/non_existing_link ->abcd.com
this php site is just hard coded, not using any framework and i wish to write script in index.php which would accept ny link towards abc.com and then check the extension and redirect it accordingly to the corresponding links on abcd.com... i was suggested to write a controller from a collegue and i am not familiar with mvc functionalities nor th controllers class and stuff...
If your webserver runs apache, I would recommend placing a .htaccess-file in the root of your original server as in the following answer: .htaccess redirect all pages to new domain. If you would like to keep the names the same (redirect to exactly the same name on your new domain), use the following lines:
RewriteEngine On
RewriteRule ^(.*)$ http://newdomain.com/$1 [R=301,QSA]
This method works with very limited amount of work/coding :-)
I believe you there are several ways to achieve so:
Using php redirect in all php pages.
How to make a redirect in PHP?
Modifying .htaccess file in your directory
http://kb.mediatemple.net/questions/242/How+do+I+redirect+my+site+using+a+.htaccess+file%3F
If you have a cpanel of other controler system from hosting provider then do it directly there.
I want to map URL in my localhost XAMPP into custom files.
For example:
localhost/index.php --> d:\xampp\htdocs\index.php (default)
localhost/normal/data.php --> d:\xampp\htdocs\normal\data.php (default)
localhost/view/userinfo.php --> d:\xampp\htdocs\view.php?p=userinfo (custom)
localhost/view/welcome.php --> d:\xampp\htdocs\view.php?p=welcome (custom)
So, basically, all URL that goes into inside view path will be mapped to view.php files with the filename.php (minus the .php) as its query parameter. There's actually no physical folder view, and no physical files userinfo.php and welcome.php inside the folder.
The reason that I need to do this is that so I can pass all the pages that viewing data into an "application frame" that will wrap the page with header, menu, and footer, and I don't need to give header, menu, and footer call in each page. I might have the actual files userinfo.php that I can $include_once, or I might not (I can just generate it from within the view.php), but hey, that's one of the power of this kind of framework, right? And, if someday I need to change this structure, I can change it from just within one file (view.php), not all.
Can I do this in PHP and XAMPP? How? I've noticed that some website seems to used this practice (URL which have no actual files or even path at all), but when I try to read tutorial for it, I got confused.
URL mapping in PHP?
The accepted answer listed 3 links to learn about URL rewriting. Mostly they're written for Apache in Linux, and mostly they pull all the possible scenario and configuration that I got confused which one I really need with all those long documents and technical jargon. I need just the practical step of my specific problem, and then, I will be able to start from there to explore myself if I have more advanced needs. Please help.
if you do want to go down the mod rewrite route adding the following to an .htaccess file in the site root should do it. You will need to make sure mod rewrite is on for XAMPP and I can't help you there I'm afraid. As you can see it rewrites the url, not the windows filename - so it would work on any OS.
The ([a-z]*) means it will take any filename.php with lowercase letters and redirect to /view.php?p=$1 where the $1 will be replaced by filename.
the [L,R] (L means last rule so stop processing if any more are reached, and the R means redirect (it will change the url in the browser). Use P instead to reverse Proxy (the user will still see the url they requested but the server will serve the correct file) - This will require mod_proxy as well.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_URI} ^view/
RewriteRule ^view/([a-z]*).php$ /view.php?p=$1 [L,R]
</IfModule>
XAMPP uses apache so the rewrites would work the same in Windows as they do in Linux. You could place a .htaccess in the site root directory with some rewrite rules.
However, using PHP
in d:\xampp\htdocs\view\userinfo.php you could include the line
<?php
header('Location: http://localhost/view.php?p=userinfo');
?>
But this must be before any thing is echoed to the screen (even whitespace).
You can use the Apache module mod_rewrite to edit requests before they hit PHP. You want to put something like the following in a .htaccess file in your htdocs directory.
RewriteEngine on
RewriteCond %{REQUEST_URI} ^view/
RewriteRule ^view/(.*)\.php.*$ view.php?p=$1 [L,QSA]
QSA means Query String Append. This means that if there are any GET parameters set on the original request they will be appended to the end of the new request too.
Note that this assumes that Apache is configured with AllowOverride enabled and the mod_rewrite module loaded.
I have a number of urls that are stored in a database, and thus instead of adding a rewrite rule in .htaccess for every url, I am using the following code in htaccess to give the control to PHP code through the following ReWrite rule in Apache:
RewriteRule ^.*$ ./index.php
A url mentioned in the database, has a corresponding original url. Though, the tricky situation comes when I have to serve the content of the url fetched from DB by the corresponding original url for which the ReWrite rules are written in .htaccess. One solution is to implement the same rewrite rules for the url fetched from DB in PHP as written in Apache for the original url, however, the number of such original urls is huge.
Thus would be glad to know about a solution if possible which can make execution flow through the ReWrite rules mentioned in Apache after the processing inside PHP is complete.
If you have access to the main httpd.conf you could use a RewriteMap written in PHP.
Other than that, there is no way you can give control from PHP back to Apache so Apache can process it further, not in the same request anyway. You could do a 30x rewrite from PHP to let Apache work on the next request.
Basic rewriting, rather create an apache rule to redirect all 404 errors to a php file, which will be your url handler, using the requested url do a lookup in your list of urls in your database, return the original url to your handler, from there either do a redirect or fetch the page contents server-side/ajax the page/iframe whichever you perfer, if the requested url is not in your list display your custom 404 page, this kills two birds.
Setting up a 404 page:
http://www.404-error-page.com/404-create-a-custom-404-error-page.shtml
I noticed in Drupal if you add .php to the url bar of any page it gives you a 404 message; clean urls enabled. The page is obviously a .php, but the .htaccess is preventing the user from being able to tamper with url extensions in the url bar. How could you do this using .htaccess. I have file extensions omitted at the moment, but would also like to add that feature. Thank you.
Also, this question does not pertain to Drupal. I only mentioned Drupal for and example.
Just because a file contains PHP code it doesn't mean it has to have the .php extension; even more so when you're accessing a file over the internet.
When you request http://mysite.com/page and you're using an .htaccess like Drupal's, the request is forwarded onto index.php?q=page whereupon Drupal will check it's database for a path matching page. If it finds one it will display the content for that page, if not it will (rightly) give a 404.
If you want all of your pages to be accessible with a PHP extension you could add an extra rule in your .htaccess file to remove .php from any request where the PHP file doesn't physically exist:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)\.php $1 [NC]
Bear in mind though that this adds zero extra value for your site's visitors (in fact they have to remember a file extension as well as the path to the page), and it exposes exactly what server-side technology you're using so a potential attacker would have some of his work done for him.
Hope that helps.
Could you please explain that in more depth. How can it redirect content into an existing page? Is that common practice / typical way of doing things?
Yes it is a very common practice, used by most frameworks and CMS.
The principle is simple: you setup your .htaccess so that every request which doesn't match a real file or directory will be redirected to a front controller, usually the index.php in the root directory of the application. That front controller handles the request by analyzing the URL and calling the necessary actions.
In this way you can minimize the rewrite rules to just one, and you can offer customized 404 pages.
I dunno Drupal but in the usual php app every request being routed to the front controller which performs some validations and throws 404 on errors.
easy-peasy
I have a very basic two-page website: the home page, and an about page. What I'm trying to do is use htaccess to rewrite the URLs so that the appear as:
domain.com/ (for the home page)
domain.com/about (for the about page)
In the actual folder structure of the site, the homepage is /index.php and the about page is /about.php (both appear in root).
I've been doing research into using alias but unfortunately my hosting (Dreamhost) doesn't allow access to httpd.config so that's out the window and I'm left with using rewrite rules in the htaccess file.
Since the index.php file will appear in the domain root (http://domain.com/) automatically, I've so far managed to make the about page appear correctly at domain.com/about using these lines:
RewriteEngine On
RewriteRule ^about$ /about.php
I'm also using 301 redirects so that for example, domain.com/about/ (with the trailing slash) also directs back to the /about URL like this:
Redirect 301 /about/ http://domain.com/about
This works great.
However the index.php and about.php files still also show if you go to the correct URL within your browser (eg: domain.com/about.php) so as a result the search engines are seeing (and indexing) two versions of each page! I've set up the correct canonical metadata within each page but this doesn't seem to have had any effect (the canonical metadata have been within the page markup ever since the site went live).
So how would I go about firstly doing a 'blind' rewrite (for want of a more technical term) for the two files so that the URLs look correct, but also effectivly 'block' direct access to the files - ensuring that if someone were to attempt to access the php files directly, the URL would still appear in the visitor's browser as the 'pretty' versions rather than the full file name and php extension?
Any advise would be hugely appreciated, I've been researching this for another couple of days now (I don't think there's anything quite the same as this anywhere on here already) and cannot for the life of me work this one out!
Check $_SERVER['REQUEST_URI'] in the PHP files. If that contains the filename (instead of the URI that you want), then do a 301 or 404 or whatever you want. Else serve the page as usual.
You can also do a
RewriteRule ^about.php - [L,gone]
or
RewriteRule ^about.php /about [L,R=301]
but this has to go before your other RewriteRules. It will send a 410 Gone or a 301 Moved Permanently response if the page is accessed via /about.php. See Apache Module mod_rewrite for the complete documentation of mod_rewrite.