Is it possible to REWRITE a URL without htaccess file? - php

Is it possible to shorten a url without using htaccess file?
for example I have this url.
this/is/a/very/long/url.php
change to
short/url.php
I hope I can get good answer THX guys:)

yes, mostly used in frameworks
an approach called using a front controller
ex: your front controller is index.php
your page links are generated as a fashion of .../index.php/nything/url.php
but the actual link is .../this/is/long/url.php
the front controller extract the page information the client requested and show the relevant page related to it
read more : http://en.wikipedia.org/wiki/Front_Controller_pattern

This is a good question, there are multiple options however the .htaccess file is probably your best bet.
this SO post describes it
Handling the url rewriting serverside is key here since it will be much faster to execute and will not break your script when used on some URL's.
so www.yourdomain.com/test/4/twenty/long/url/could/be/shorter/
all the arguments after www.yourdomain.com, can be retrieved via various PHP methods, including reading up on PHP's $_SERVER would be a good idea, as lots of variables are placed in that global array.

mod_rewrite is part of Apache, so it needs to be configured in Apache config. .htaccess actually Apache "live" config "per directory".
In general the best way ( I think) to rewrite urls is to rewrite everything to one file, let's say index.php and then "redirect" to specific file basing on URL. You can read a lot about "URL routing" in PHP on the web.

You can use this code in Javascript for rewriting current URL:
if (location.href.indexOf("this/is/a/very/long/url.php") > -1)
location.assign(location.href.replace(/this\/is\/a\/very\/long\/(url\.php)/, "short/$1"));

Related

Is there a way to rewrite my urls without using mod_rewrite and .htaccess?

Is there a way to rewrite my urls from:
http://www.website.com/notification.php
to
http://www.website.com/notification
I dont have the permission to turn on mod_rewrite and our organisation doesnt want it either. Is there a way to realise this with php only?
Thanks in advance
If you have only a few pages, you could rename your files index.php and move it inside directories with your desired name.
So notification.php becomes notification/index.php and you could access it with the url /notification or /notification/.
Downside of this method is, you get a messy folder structure and every file is named index.php. You could come around the later issue, if you put some kind of wrapper file inside each folder, which calls the actual script - but the folder structure stays the same.
An other way would be to use some kind of pseudo rewrites with urls starting with ?. So you could call access your files like website.com/?notification. In this case, you have to put all your routing logic inside an index.php located at /. The good part about this is, that your urls stay flexible and you don't have to create some strange file system structure. Bad point is, you have to keep the question mark in each url.
HTML5 introduce history.pushState()
pushState() takes three parameters: a state object, a title (which is
currently ignored), and (optionally) a URL.
Example :-
URL :- http://www.website.com/notification.php
window.history.pushState('notification.php', 'Title', 'notification');
I do believe you can do this "simply":
<Location "/notification">
SetHandler application/x-httpd-php
</Location>
If you do this I hope you're using a flexible request router as you'll be using REQUEST_URI a lot. As apache won't bother looking for files or anything else AFAIK, so you'll have to return static resources (images, stylesheets, js, etc) yourself.
Regardless, it seems like you can use this in a .htaccess file so you don't need to bother the server's main configuration.
See: https://httpd.apache.org/docs/2.4/mod/core.html#sethandler

PHP Filename Parameter

I have seen various sites now defining the products name in the URL ... for example
http://www.webesite.co.uk/hp/hp-c9701a-cyan-toner-cartridge-original
instead of
http://www.webesite.co.uk/product.php?product=hp-c9701a-cyan-toner-cartridge-original
I am aware and have used a lot the $_GET function to get parameters specified in the URL after the .php file tag, but my question is how do you provide a parameter to replace the file name rather than specifying it at the end of the file name?
I'm working in PHP but I'm not sure if this is achieved using the .htaccess file
I am also interested to know which will perform best SEO wise???
Yes you need to use the mod_rewrite with .htaccess to redirect to the correct resource.
Here is some usefull link that explaim better what you actually need to do in .htaccess:
http://zenverse.net/seo-friendly-urls-with-htaccess/
And here is a similar thread in Stackoverflow:
How to create friendly URL in php?
It's usually done by redirecting every request to index.php using .htaccess and than by parsing (explode()) URL you can get every part of it and treat as a parameter.
You can also read about routing and router classes which are designed to do this work for you.

A Dynamic website using PHP

I am a beginner PHP programmer. I searched google for a "Dynamic PHP website tutorials". I found some stuff. They use $_GET variable to make the website dynamic, so the URL's appear like this:
example.com/?page=home
example.com/?page=about
example.com/?page=Downloads
and so on...
But most of the dynamic websites that I found on the internet has links like this:
example.com
example.com/about
example.com/download and so on....
So how do they do so ?? Have they got folders for all the catogories ?? And Also some websites have article URLs (eg : example.com/articles/posts/2010/article1.php). It would be a reall mess if they've got folders for all items. If not then How ?? Can someone give an example please ?
If you're using apache then read: http://httpd.apache.org/docs/current/mod/mod_rewrite.html
If you're using IIS then read: http://www.iis.net/downloads/microsoft/url-rewrite
In order to use the $_GET variable, it must be in the query string (or being routed through some other means that isn't 'default').
For example, the URLs you're using would become.
example.com/?page=home
example.com/?page=about
example.com/?page=Downloads
Additionally, you can rewrite URLs using the .htaccess file (http://httpd.apache.org/docs/2.0/misc/rewriteguide.html)
You are interested in page routing.
htaccess and MVC routing may start you down the correct path :)
To echo everyone else, it's called a url rewite.
For example, the url
http://example.com/index.php?ext=blog&cat=news&date=12122012
can be rewritten as
http://example.com/blog/news/12-12-2012
This isn't automatic, it requires defining the patterns used for understanding the new URL in a file called .htaccess which usually resides in the servers root directory. Note that the preceding '.' in the filename makes it a hidden file.
When I was first getting used to PHP i found the site http://phpbuilder.com a great help. They have a lot of articles, and a forum that is fairly nice to noobies. http://devshed.com is a good site too, and has a large amount of information on subjects outside of PHP.
You can achieve that affect with folders, but most use rewrites (Apache). It's a bit too broad of a subject to go in to here, but if you just search for rewrite tutorials you'll find some pretty quickly.
The $_GET is only to get variables from the URL. While this can be used to make sites dynamic, this is a technique which is usually frowned upon.
With rewrites, you basically have a URL like /about, but the rewrite tells your server something like "act like this is actually ?page="about"), which you then use the $_GET to process.
Being PHP beginner I will not urge you to use .htaccess, As you will need to learn lot many things before you proceed further. You have 2 option to send a request one is GET and POST. You can get more information about same on internet.
Also you have an option to start your dynamic website using CMS and I will recommend you to use wordpress. CMS will have some in-built function which will help you to do your work faster. Also using their control panel you can update the URL format.
I will also urge you to go step by step and follow every tutorial that you will find on internet.
All the best
If you want to do this you have to use .htaccess file and have to load mod_rewrite in your apache server.
In you root directory create file named .htaccess
Then Write:
RewriteEngine On
RewriteRule ^(.*)\.php$ index.php?page=$1 [L,QSA]
And After that call a page
my-page.php
It will redirected as a index.php?page=my-page internally but in browser it will show as my-page.php

URL GET request formatted as a deep link

I'm wondering how data passed to the URL can be "camouflaged" as a deep link. Here is an example of a site that does this: http://www.conradit.no/
I actually developed that site, but paths have been changed later.
So I have seen sites that does this even without referencing the index page (e.g. www.sometime.com/get/data/camouflaged)
I have seen some jQuery libraries that does this using anchor tags, but that is out of the question since users of the CMS I'm writing may want to use anchor tags on their pages.
I would like to know how this is done all server side using php and .htaccess files or such.
Thanks for all replies!
They use a rewrite engine, such as Apache's mod_rewrite.
The site is using some URL rewriting techniques. By the look of the URL path and the Server header field in the response I guess they’re using the so called path info (see Apache’s AcceptPathInfo) to have requests like /index.php/foo/bar redirected internally to /index.php with the path info /foo/bar.
But they could also use other techniques that can rewrite/redirect the requested URL based on patterns like Apache’s mod_rewrite.

Possible to remove extentions on php pages?

When I go to some websites I notice that there is no file extension on the page. Actually, this site is a proper example. Anybody know how this is done? :]
This is generally accomplished with URL Rewriting (link goes to a great introductory tutorial).
In the simplest case, a rewrite rule simply redirects every address by adding ".php" (or whatever other file extension) on the end of the url. So if someone tries to visit http://www.yoursite.com/about-me, they are actually viewing the content coming from the file about-me.php.
This could also be an MVC framework. (Search here, there are lots for php, a famous one for .NET, and I am sure many frameworks for many language.
This site has a root file, lets call it index.aspx (its not, but go with me) and that file has special code to read more of the URL. Used in combination with URL rewriting as mentioned, that special file takes in what would normally be path information and treats it as variable input.
Possible to remove extentions on php pages?
is something like
https://stackoverflow.com/index.aspx/questions/778799/possible-to-remove-extentions-on-php-pages
With index.aspx being the file that is hidden via URL rewriting.. Questions is a parameter indicating the controller, 778799 being a parameter that and the other text being the final parameter. (That is ignored)
For more see
http://www.asp.net/mvc/
http://codeigniter.com/
http://en.wikipedia.org/wiki/Model-view-controller
Aside from URL rewriting, you can also change apache's configuration to support alternative file extensions allowing you to do do something like MyPHPPage.html.
Regards,
Frank
Use mod_rewrite. Just put code like this in a file called .htaccess on your root:
RewriteEngine on
RewriteRule ^([^/\.]+)/?([^/\.]+)/?$ index.php?resource_type=$1&id=$2 [QSA,L]
Notice how $1 and $2 access the two quantities in parenthesized portions of the regular expression. It seems easy to extend the code from here

Categories