How do you pass a variable to another page with a URL like the one below?
www.domain.com/results?search_query=test
Instead of like this:
www.domain.com/results/?search_query=test
(referring to the slash after "results")
I think the first URL looks nicer, but each time I try to have it that way on my site, it automatically puts a slash in.
Currently, my file path looks like so root>results>index.php (file that I pass the variable to)
Instead of making a folder called results with index.php inside it, make a php file called results.php in the root directory.
Then using .htaccesss file, you can rewrite the link to fit to what you would like.
RewriteEngine on
RewriteRule ^([^/]*)$ $1.php
Related
I have this situation:
There are many scripts that can be accessed like this:
http://website.com/script1/
http://website.com/script2/
Each script has a display.php file that receives a variable from the index.php file VIA a link. I'm using the $_GET method to reat that variable.
Example:
http://website.com/script1/display.php?id=124
What I'm trying to do is to access the display page like this:
http://website.com/script1/124
http://website.com/script2/124
Does anyone have any suggestions? My experience with htaccess is ~0.
RewriteRule script[\d]+\/[\d]+ /script$1/display.php?id=$2
This would rewrite anything like script345987/22352 to script345987/display.php?id=22352
Edit if you want to match anything, not only script, then:
RewriteRule (.*)\/[\d]+ /$1/display.php?id=$2
I am writing the following rules in htaccess file to change the query string as directory structure
RewriteRule ^dashboard/([0-9]*)?$ dashboard.php?user_id=$1
is used to rewrite the url. It is working fine on
localhost/project/dashboard // (dashboard.php)
and all links are as
localhost/project/css/style.css
localhost/project/js/script.js
But When I append an id
localhost/project/dashboard/1 // (dashboard.php?user_id=1)
It changes all the links as
localhost/project/dashboard/css/style.css
localhost/project/dashboard/js/script.js
Why it is appending the dashboard to all links
How is the style.css referenced in your html file?
If you have it like this href="css/style.css", the HTML doesn't know you're rewriting, thinks /1 is a folder and will look in dashboard/css/style.css
Any system that uses url rewrite usually has to write all the path to the styles and scripts to avoid this. so you will have to reference your style like this
href="http://localhost/project/css/style.css"
if you have a production and development environment it will help you to have a variable like
if($SERVER['SERVER_NAME']=='localhost'){
$BASE_URL = "http://localhost/project/"
}else{
$BASE_URL = "http://mydomain.com/"
}
and put that before any call to css, scripts or images
;)
It's because you "tell him" to do that.
RewriteRule ^dashboard/([0-9]*)?$ dashboard.php?user_id=$1
// ^here you tell him to print that "dashboard"
Of course other links works - you don't even match them with that rule.
I found you something here, scroll down to the title "Strip Query Strings". There, it say you to do this:
RewriteCond %{QUERY_STRING} example=
RewriteRule (.*) http://www.domain.com/$1? [R=301]
Just, of course, change that url to your own.
I have a URL that is like the following:
http://www.example.com/client/project/subdirectory/value/
I would like like a simple way to be able to change/redirect the URL to the following:
http://www.example.com/client/project/#/subdirectory/value/
Once the redirect is complete, the hash needs to be accessible via JavaScript. I'm okay with a full refresh/redirect, just ideally that I write this once and don't have to change it again.
In other words, when the site goes live, the URLs will be structured differently, so that:
http://www.example.com/subdirectory/value/
Will change to:
http://www.example.com/#/subdirectory/value/
Edit:
I have tried using this:
RewriteRule ^profile/?$ #/profile/ [ NC,L]
Which doesn't seem to do anything
Also tried this:
RewriteRule ^profile/?$ /#/profile/ [NC,L]
Which takes me to the root directory
Also tried this:
RewriteRule ^profile/?$ #/profile/ [R,NC,L]
Which adds the whole root path to the server, followed by /%23/profile/
If you have a URL like the following:
http://localhost/tests/redir/subdirectory/value/
And you want to get it redirected to:
http://localhost/tests/redir/#/subdirectory/value/
Place a .htaccess file into the directory of tests/redir with the following:
RewriteEngine On
RewriteBase /tests/redir
RewriteRule ^(.*/)$ #/$1 [R,L,NE]
And you will get the wanted redirect. The R flag plays together with the RewriteBase directive. Also the NE flag is necessary so that you can put # literally into the redirect URI.
The hash is already used by page anchor tags. You might need to replace it with an entity or pick a better character.
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']
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