HTML URL shortcuts instead of page name - php

I am not exactly sure the exact words to google.
How do I rename the url link to a simpler one for my webpages?
example:
from:
websitename.com/about_us_what_we_do.php
to:
websitename.com/what-we-do
another example is to let the homepage just show the address
from:
websitename.com/main.php
to
websitename.com/

You can use url rewriting using .htaccess
In .htaccess you have to mention the url that what you want & which will redirect you to actual page.
Exp :
browser url will be: domain_name/what-we-do
& Actual url will be : domain_name/about_us_what_we_do.php
in .htacces you have to mention :
RewriteRule ^what-we-do$ /about_us_what_we_do.php [L]
Check this ref:
I have short the URL Using .htaccess but every shortened URL goes to the same page

This can be done by creating a subfolder with the name what-we-do
and renaming your file about_us_what_we_do.php to index.php.
Then, you'll have your site root directory. Inside you'll have a folder named what-we-do with an index.php file inside.
If you want to do things right, you should look into VirtualHosts ( assuming you're managing the Apache server)

Related

.htaccess redirection from sub folder to current folder with with query parameters

I have index.php with query pi which looks like this : index.php?pi=1
.htaccess code is the following :
RewriteRule ^([0-9]+)/$ index.php?pi=$1 [NC,L]
which was supposed to make it look like /current_folder/ , /current_folder/2/, /current_folder/3/
This is causing duplicated issue in Google as : /current_folder/ 's content is equal to /current_folder/1/.
I need to redirect all /current_folder/1/ to /current_folder/, and however leave /current_folder/2/, or higher, as they are.
Problem solved via php solution using if else structure, i.e. when we are on page no.2 view /current folder/ for previous. all good.

Redirection from the php file

I'm looking for a little help from someone who knows PHP,
in short, it looks like this:
I once made a website for a customer with products on the Jomla CMS system, and the product was located, for example, at:
www.mysite.pl/index.php/productall/product1
www.mysite.pl/index.php/productall/product2
e.t.c...
currently his site has been changed and is in the classic html5 / css / js without any CMS, but in the same old directory
and the product is on:
www.mysite.pl/product1.html
The client created QR codes for himself, but to the old page and printed on each product
and now I would like to create an index.php file, which will redirect the traffic depending on the call in the browser, because now as the QR code is being scanned it jumped out because there is no such adreas as before.
For now, I created an index.php file that redirects to
www.mysite.pl/allproduct.hml
where is the list of all products but I would prefer it to be as before
that is, each QRcode was redirected to the chosen product.
Can you do it somehow? PLEASE HELP
now i have this code in index.php
Instead of messing with PHP, You could try adding redirect commands within the htaccess file? (if you can still use one?) The ".htaccess" file should be located in the site root.
ie: /public_html/.htaccess
within this file you can create redirects from the old address to the new one.
Redirect 301 /oldProductLink1.html http://www.example.com/NewProductPage1.html
Redirect 301 /oldProductLink2.html http://www.example.com/NewProductPage2.html
:)
There's no easy solution you must create a .htacess file and place on your root folder then redirect all the old urls to new url for each product
Redirect 301 /index.php/productall/product1 www.mysite.pl/product1.html
Redirect 301 /index.php/productall/product1 www.mysite.pl/product2.html
Redirect 301 /index.php/productall/product1 www.mysite.pl/product3.html
Redirect 301 /index.php/productall/product1 www.mysite.pl/product4.html
If the question is only about PHP and there is no way to use web server URL rewriting or redirect, then you should create files like :
/index.php/productall/product1
/index.php/productall/product2
Where index.php would be a directory. Each file should contain something like :
header('location: /product1.html');
exit();
Where the product number should be set according to file name.
i still have this:
The resource you are looking for has been removed, had its name changed, or is temporarily unavailable.
I removed this my test index.php file and created .htaccess in the root directory with yours codes

Redirect direct access to files in subfolder to script

I have a subfolder that holds user uploaded files. I want to redirect all direct file requests to this folder to another .php script...so i can check if the user is logged in before i send/show the file to him.
For example:
/mainsite/uploads/user/2324/file.pdf
needs to be forwarded to
/mainsite/uploads/permissions.php
But i need inside the permissions.php to be able to do:
$url = $_SERVER['REQUEST_URI'];
and see what was the initial request...in order to readfile() the file after all the 'checking'.
I've tried this:
RewriteEngine On
RewriteRule ^/uploads/user/?$ /uploads/permissions.php [R=301,L]
but this is just a simple forwarding...i got no idea what file or folder the user requested.
I know i can do this by creating an individual htaccess file inside every folder that is created under 'user/{userid}' but i wanted a simpler function. I dont want to have 10000 htaccess files, if i can do this with just one.
Thanks
try with this syntax: (i added the R=301 part, which wasn't necessary in my version, so it is not fully tested, works without the R option)
RewriteRule "^/uploads/user/(.+)$" "/uploads/permissions.php?file=$1" [R=301,QSA,L]
You can the get your file var in the $_GET array in permissions.php. However, i wouldn't recommend to use directly this value because it can be unsecure. The best way is to only allow fixed values, with a switch for example, having filtered the var as a string before.

what is the best way to do that name?get=

ok assume i have php page
has this name name.php?get= and has get varible named get
ok
how i can make it appear like that name?get=
If you are using apache, mod_rewrite is one way to go. There is a whole bunch of mod_rewrite tricks here.
I'd seriously reconsider before using (or overusing) mod_rewrite.
In almost all of my projects I use a simple mod rewrite in the .htaccess:
RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^.*$ ./
AddHandler php5-script .php
This tells the server to forward all pages to / (index.php) unless a file otherwise exists.
In the root directory I have a folder called "views" with all of the pages that I use. E.g. the file used for /home would actually be /views/home.php. However, in the index.php I have a script that parses the user's url, checks for the file, and includes that.
$page = substr($_SERVER['REQUEST_URI'], 1);
if(!$page) :
header("Location: /home");
if(file_exists("views/$page.php")) :
include "views/$page.php";
else :
include "views/$page.php";
endif;
This creates a variable called $page that stores the value of everything in the URL after the domain name. I use a substr() function on the Request URI to get rid of the trailing forward slash (/) on the URL.
If the variable is empty, for example if the user is simply at http://example.com or http://example.com/ then it forwards them to /home, where the script then checks for the home.php file inside of the views folder. If that file exists, it includes it, and displays it to the user.
Else, the script will simply include the 404 page telling the user that the file doesn't exist.
Hopefully this helps you, and if you need any further explanation I'd be happy to help!
I think you're wanting to re-write the URL client-side, which would include mod_rewrite.
In the route of your website, create a file called .htaccess and place the following code in it:
RewriteEngine on
RewriteRule ^name?get=(.*) /name.php?get=$1
Now when you type http://www.example.com/name?get=something, it will actually map to http://www.example.com/name.php?get=something transparently for you.
As far as i could understand your question, you can not strip the file extension because otherwise it will not run. In other words, you can not change:
name.php?get=
into
name?get=
But if you mean to create links with query string values that you can put them in hyperlinks in this way:
Click here !!
If you're looking to create links using a variable '$get', then you can create the link like this:
<a href="name.php?get=$get>Link</a>
Or if you want to get the value of the query string variable, you can use this:
$get = $_GET['get']

How To rewrite a url with PHP?

Let' say for example one of my members is to http://www.example.com/members/893674.php. How do I let them customize there url so it can be for example, http://www.example.com/myname
I guess what I want is for my members to have there own customized url. Is there a better way to do this by reorganizing my files.
You could use a Front Controller, it's a common solution for making custom URLs and is used in all languages, not just PHP. Here's a guide: http://www.oreillynet.com/pub/a/php/2004/07/08/front_controller.html
Essentially you would create an index.php file that is called for every URL, its job is to parse the URL and determine which code to run base on the URL's contents. So, on your site your URLs would be something like: http://www.example.com/index.php/myname or http://www.example.com/index.php/about-us or http://www.example.com/index.php/contact-us and so on. index.php is called for ALL URLs.
You can remove index.php from the URL using mod_rewrite, see here: http://www.wil-linssen.com/expressionengine-removing-indexphp/
Add a re-write rule to point everything to index.php. Then inside of your index.php, parse the url and grab myname. Lookup a path to myname in somekinda table and include that path
RewriteEngine on
RewriteRule ^.*$ index.php [L,QSA]
index.php:
$myname = $_SERVER['REQUEST_URI'];
$myname = ltrim($myname, '/'); //strip leading slash if need be.
$realpath = LookupNameToPath($myname);
include($realpath);
create a new file and change it name to (.htaccess) and put this apache commands (just for example) into it :
Options +FollowSymLinks
RewriteEngine on
RewriteRule ^profile/([0-9]*)$ members.php?id=$1
You must create a rewrite rule that point from http://www.example.com/myname to something like http://www.example.com/user.php?uname=myname.
In '.htaccess':
RewriteEngine on
RewriteRule ^/(.*)$ /user.php?uname=$1
# SourceURL TargetURL
Then you create a 'user.php', that load user information from 'uname' GET variable.
See from your question, you may already have user page based on user id (i.e., '893674.php') so you make redirect it there.
But I do not suggest it as redirect will change the URL on the location bar.
Another way (if you already have '893674.php') is to include it.
The best way though, is to show the information of the user (or whatever you do with it) right in that page.
For example:
<?phg
vat $UName = $_GET['uname'];
var $User = new User($UName);
$User->showInfo();
?>
you need apache’s mod_rewrite for that. with php alone you won’t have any luck.
see: http://httpd.apache.org/docs/1.3/mod/mod_rewrite.html

Categories