This question already has answers here:
How can I remove file extension from a website address?
(11 answers)
Closed 9 years ago.
I have a build a few sites for my website. They all end with .php.
The problem is if you want to view the page you have to type in website.com/page.php instead of website.com/page. How do I make this happen for all of my main pages?
Is there a quick way of doing this, or do you have to set up a forwarding for all of the /pages to the /page.php?
In most cases this is achieved using MVC framework and Routing. It works in a way that you don't access single .php file for single web page you show to user. Every request goes through one file and you have a router where you define your routes and then define what action controller would that route invoke, and from there you choose what view file will you show to the user. Its hard to explain in few sentances. Anyway using MVC you get nice URL-s like www.example.com/controller/action/param
Now if you just want to remove .php extension from your files you can put this in your .htaccess file:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
But other then hiding .php, it wont do any good.
The best thing you can do is to read about MVC frameworks, Routing and Front Controller pattern, and take it from there, its not all about nice URL-s, there's much more to gain! And if you just want to hide .php extension then use above code.
Hope this helps!
You save your first file as index.php (index.php is the default page) and include or redirect all the other files internally. So there would be no reason to type a file name.
You can also use apache on .htaccess to rewrite your files, but you have to be careful with this.
Related
I currently run a site with 750 pages of .html webpages (yeah I know it was a stupid idea, but I'm a novice). I'm looking to move these to php. I don't really want to set up 750 individual 301 redirects and rewrite each page to .php
I've heard that I can use htaccess to this. Anyone know how?
A few additional questions -
Can I permanently redirect these links from html to php without losing my search engine rankings and
if I want to add php to each of the files (i.e. a php file menu (using the include command) to make the links quicker to update will this work? Because won't they still be html files?
Sorry for the stupid questions, but I'm still learning.
Congratulations on a 750 page site - you must have put some work into that.
To collect your current list of pages use a tool called xenu to create an export into excel. You can then easily change the name the files to PHP in column b and create a .htaccees file.
However why would you want 750 php files? If you have lots of data pages, make it one page and suck in the HTML main content and reference one page. If you have a page called warehouse-depot-22-row-44.html then change that to show-warehouse-row.php?depot=22&row=44 and return that content only. This will significantly reduce your number of pages and to start using databases to render the content.
For redirecting you could use the Apache Module mod_rewrite: https://httpd.apache.org/docs/current/mod/mod_rewrite.html
You can use url rewriting to match a specific file name request with a regular expression and then decide where to redirect if matched
RewriteRule ^myname/?$ myname.php [NC,L]
http://www.addedbytes.com/articles/for-beginners/url-rewriting-for-beginners/
Depends on the structure you have.
You want the user to access them in their natural location?
/public_html/folder1/file.php
user would access like
mydomain.com/folder1/file
or you want to map them differently?
Personally I think I would use a rewrite rule to map all requests to my /public_html/index.php and would map the requests from there using php (using include for instance). This gives great flexibility, plus you have a single point of entry for your application which is very beneficial since you can easily maintain control of the application flow.
The .htaccess would look like this
#
# Redirect all to index.php
#
RewriteEngine On
# if a directory or a file exists, use it directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# RewriteCond %{REQUEST_URI} !^/index\.php
# RewriteCond %{REQUEST_URI} (/[^.]*|\.(php|html?))$ [NC]
RewriteCond %{REQUEST_URI} (/[^.]*|\.)$ [NC]
RewriteRule .* index.php [L]
of course I place all my not directly accessible files (everything except index and css, js, images, etc) to a folder outside the public_html to ensure no user can ever access them directly ;)
I've had a similar (yet much much smaller) site that went through the same thing.
I have this in my .htaccess:
RewriteEngine On
RewriteRule ^(.*)\.html$ $1.php [L]
This will help redirect any visitors to your .html addresses to your .php addresses.
You hopefully have an IDE (I recommend Aptana), and you can use some of the find/change functions project-wide, and hopefully with some time and patience get your internal links from .html to .php.
But, I caution you a little bit - Perhaps it is time to look into a database based CMS, such as Wordpress or Drupal?
i am building a web site using PHP and TWIG, i have organized my code into:
class folder: for php classes
lib folder: for non-classes php files
templates folder: for twig templates
and index.php file
when i want to include a link for register page for example the link will be: domain-name/lib/register.php
the question is: is there any good way of hiding the file organization from the link
for example to make the link something like: domain-name/register without changing my file organization and preserving the ability to send get parameters in the url?
thanks
If you are using apache, you can use the mod_rewrite module with a .htaccess file
http://httpd.apache.org/docs/2.0/mod/mod_rewrite.html
For other webservers similar modules and methods exist.
Here's an example htaccess file I would use. Folder structures entered after "register/" are treated as variables that forward to register.php. The QSA flag will allow you to have additional GET variables treated as vars if need be. (e.g. /register/something/?some_var=1).
This is for specific cases when you know how many variables you want to rewrite for what pages. In other words, the below will only work with two variables/spots (e.g. /registers/var1/var2/).
Hope that helps!
############################################
## ENABLE REWRITES
RewriteEngine on
Options +FollowSymLinks
## EXAMPLE WITH 1 VAR
RewriteRule ^register/([A-Za-z0-9-]+)/ lib/register.php?var1=$1 [L,QSA]
## EXAMPLE WITH 2 VAR
RewriteRule ^register/([A-Za-z0-9-]+)/([A-Za-z0-9-]+)/ lib/register.php?var1=$1&var2=$2 [L,QSA]
## ALSO GOOD TO HAVE ERROR DOCS REWRITE
ErrorDocument 404 oops/
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
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
How to create friendly URL in php?
I have read a lot of content on Google about turning
profile.php?id=24 into something more user friendly.
How would I go about redirecting a link like
www.site.com/profile/username
to
profile.php?id=userid (find from username)
Thanks
This can be achieved with the Apache mod_rewrite RewriteEngine. An example .htaccess:
RewriteEngine On
RewriteRule ^/profile/username/([\d]+)$ profile.php?id=$1
You can do that with apache rewrite rules.
Apache will internally rewrite a URL like /profile/username to profile.php?username=username.
Example: (place this in a .htaccess in the same directory than profile.php)
RewriteEngine On
RewriteRule ^/profile/(.*)$ profile.php?username=$1
If you profile.php script doesn't accept a username parameter, you could also include the user id in the user friendly url:
RewriteEngine On
RewriteRule ^/profile/(.*)-(\d+)$ profile.php?id=$2
This will rewrite urls like /profile/username-id to profile.php?id=id
See apahe mod_rewrite documentation.
This is done by creating a RewriteRule in a .htaccess file, of by defining rules in httpd.conf.
This works on Apache. I'm, not sure how to do this on other servers.
When your users sign up, use a PHP or CGI script to make a file called "Username.txt" and store it in a folder called Profiles (as you have it). And inside the text file, make it generate the number (counting up or hashed?) Or use a rewrite service in Google or Apache.
I just inherited a website built in PHP. The main page of www.mysite.com has a href to www.mysite.com/index/35.html somewhere in the page. In the site's root directory and its children there is no document 35.html.
The number 35 is actually an id found in a DB which also holds the html contents of the page.
If I load URL: www.mysite.com/index.php?id=35 the same page loads.
How does PHP know how to automatically convert
/index/35.html
to
/index.php?id=35
EDIT
Based on the answers, I have found a .htaccess file containing rewrite instructions that would explain the functionality.
However, IIS doesn't seem to (or is not configured) know how to use this. (probably because this is an Apache feature?)
So this begs the following question: Is there a way to configure IIS to work with this?
it will be done usign URL Rewriting using .htaccess - should be in the webroot.
It may look something like:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php [QSA,L]
May have other bits, but what this basically tells apache is to send anything that DOES NOT physically exist to index.php
It doesn't. There is a mod_rewrite rule that rewrites from /index/foo to /index.php?id=foo, either in a .htaccess file somewhere or in the httpd configuration itself.
RewriteEngine On
RewriteRule ^index/([\d]+)\.html /index.php?id=$1 [NC,L]
This is off the top of my head. Any browsers trying to load an address starting with index/ has any number ending in .html will be internally redirected to index.php?id= whatever the number is.
Edit: Just saw that your working on IIS. This probably won't work for you. Sorry.
I think you will be using .htaccess to redirect all requests to index.php. From there You can pass the query string a routing class, which will parse the url and identify the unique ids.
In this case we can say like, your routing class will parse the request /index/35.html to indexController, indexAction, id=35. now you can pass this id to the model to get corresponding page contents
NB : Here I a am assuming you are using mvc pattern. Anyway it can be treated in your own way, with the concept remaining the same. Hope this make sence.