i want to redirect
http://testsite.com/campus.php
to
http://testsite.com/campus.php
but the file campus.php doesn't exists. It fetches the page content from database and it must be displayed.
I haven't got any idea.
First of all, your two URLs are the same.
But if you want to call a page that doesn't actually exist but is pulled from the database, you typically redirect some part of the URL to point into a $_GET variable used to access the database. All requests actually go to index.php, and index.php handles the database and displays the correct data.
# Conisde this pseudocode
# Rewrite somepage to index.php?pagename=somepage
RewriteRule /somepage.php /index.php?pagename=somepage
# The actual .htaccess rewrite looks like:
RewriteEngine On
# Assuming pagename is upper/lower letters and numbers only...
RewriteRule /([A-Za-z0-9]+)\.php /index.php?pagename=$1
Now in your PHP, you use $_GET['pagename'] (campus in your case, I think) to call the text from the database and display it.
EDIT I added the \.php to the RewriteRule. Now /campus.php rewrites to /index.php?pagename=campus
Related
I have this structure on my WebServer:
\main
index.php
The index.php file is accessible with www.example.com/main/.
Now I want to redirect every query with a sub folder structure (of not existing folders) to my index.php file and pass the entered URL.
Like:
www.example.com/main/world/fish
www.example.com/main/hasuisdg
www.example.com/main/bowl/soup
www.example.com/main/salat
All these URLs should be answered by my index.php while passing the URL.
General example:
Code of index.php:
<?php
echo passedurl(); //this is the function I am looking for
?>
Entering www.example.com/main/ilikestackoverflow in browser.
Showing www.example.com/main/ilikestackoverflow on webpage.
Not absolutely important:
The URL changes to www.example.com/main/.
Not absolutely important: The url changes to www.example.com/main/
You can easily do this with a front-controller pattern, except for that last bit (if that is indeed an optional requirement or undesired behaviour?). If all the URLs "changed to" /main/ then you can't easily route the request appropriately - it would be impossible to bookmark - and it would confuse users.
You've not stated where you want the .htaccess file to go. I'll assume you want it in the root of your site. (You could alternatively have it in the /main subdirectory - but you would need to change the directives slightly.)
For example, using mod_rewrite to internally rewrite requests for the URL /main/<something> to /main/index.php, where /main/<something> does not map to a directory, try the following:
RewriteEngine On
RewriteBase /main
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^main/[^.]+$ index.php [L]
The regex ^main[^.]+$ matches URLs that do not contain a dot - so this naturally excludes requests that look-like files. So, I have excluded a check for existing files (only for existing directories - as stated).
Then, in index.php you can examine the $_SERVER['REQUEST_URI'] superglobal to access the requested URL. There isn't necessarily a need to explicitly pass the requested URL to your script. Note that the PHP variable $_SERVER['REQUEST_URI'] also contains the query string (if any). (The RewriteRule pattern matches against the URL-path only, which notably excludes the query string.)
In my php page I have a hyperlink like:
Remove User";
The delete_user_page.php via GET takes the name of the user and executes the script. I want a URL rewrite mechanism inorder to hide the passed variable. So I wrote:
.htaaccess
RewriteEngine On #Turn on the re-writing engine
RewriteRule ^delete/?$ delete_user_page.php?name=$1 [NC,L] # Handle requests for "Delete users page"
When loading this onto the server and refreshing the results nothing has changed/showed so I realized I am doing something wrong.
Where does htcaccess have to be located relative to delete_user_page.php?
Looking at "delete_user_page.php?name=$1", is $1 correct or should it be different?
If there is something else wrong in my script please tell me.
I need all subdomains to be redirected to a specific page, without actually changing the URL, because I will display different content on this specific page depending on what subdomain is in the URL.
Let's say my site is located at testdomain.com/site1/
I want all subdomains, like xyz.testdomain.com/site1/ or even xyz.testdomain.com to be redirected to a specific page at http://testdomain.com/site1/index.php/test.php
The browser will then need to be loading http://testdomain.com/site1/index.php/test.php, but the URL will still be xyz.testdomain.com.
The purpose of this is so that someone can go to abc.testdomain.com or xyz.testdomain.com and both will take the user to testdomain.com/site1/index.php/test.php, and then on test.php, I have some code that will grab the URL, and if the url is abc.testdomain.com, it will display certain content, whereas if the subdomain is xyz.testdomain.com it will display different content.
Is this something I can do in htaccess? If so, how?
Using mod_rewrite you can hack this together.
# Step 1: If the user went to example.com or www.example.com
# then we don't want to redirect them. (S=1 says skip the next rule)
RewriteCond %{HTTP_HOST} ^(www\.)?example\.com
RewriteRule ^ - [S=1]
# Step 2: Anything else is redirected to our catcher script.
# Option 1: keeps the path they went to, but discards the domain
# i.e. xyz.example.com/abc/def.txt => /var/www/cgi-bin/abc/def.txt
RewriteRule ^/?(.*) /var/www/cgi-bin/$1 [QSA,L]
# Or Option 2: take all requests to the same file
# i.e. xyz.example.com/abc/def.txt => /var/www/cgi-bin/myfile.php
RewriteRule ^ /var/www/cgi-bin/myfile.php [QSA,L]
QSA tells it to forward the query string, L tells it to stop looking for more redirects (not absolutely necessary but sometimes helps if you have a lot of this sort of thing going on).
You can also pass variables to your script as query parameters, and the QSA flag ensures they don't replace the original values;
# xyz.example.com/abc/def.txt => /var/www/cgi-bin/myfile.php?host=xyz.example.com&path=/abc/def.txt
RewriteRule ^/?(.*) /var/www/cgi-bin/myfile.php?host=%{HTTP_HOST}&path=/$1 [QSA,L]
It means you don't need to worry about figuring out where the request came from inside your script (which might actually be impossible, I'm not sure). Instead you can just read it as a normal parameter (it's hackable like a normal parameter too; be sure to sanitise it).
I am trying to use mod_rewrite to redirect users keeping the multiple query string and creating a redirect page
For Example,
If user opens
http://localhost/url/url/http://www.google.com/contacts/?user=abc&stackoverflow=great&google=facebook
then he is taken to
http://localhost/url/url.php?redirect=http://www.google.com/contacts/?user=abc&stackoverflow=great&google=facebook
There is secondary problem that URL should be encoded and then redirected! If URL is not encoded then the string (&stackoverflow=great)would be not a part of 'redirect' string of url.php
I tried many solutions then came for stackoverflow! I tried the following code in following file
http://localhost/url/.htaccess
RewriteRule ^url/([^/])$ url.php?redirect=$1 [QSA,L]
but the result is localhost/url/url.php?redirect=http only
Your setup won't work with the unencoded inner url, so an 'answer' can only have temporary character. But this might be a starting point:
RewriteEngine on
RewriteRule ^/url/url/(.*)$ /url/url.php?redirect=$1 [L,QSA]
I wonder however if that fragment /url/url is really intended (the two 'url's in there).
Note that the exact rule content also depends on where you want to define that rule. The syntax is different whether you use the central server configuration (referred) or .htaccess style files (as second choice and more complex).
Try this
RewriteEngine on
Redirect ^url/url/(.*)$ url/url.php?redirect=$1
The basic redirect systax,
redirect accessed-file URL-to-go-to
i was given 3 static pages e.g
proposal.test.com/seo
proposal.test.com/ppc
proposal.test.com/design
I checked those directories in the server and there's no dynamic about their indexes, all plain htm file.
the instruction given to me was, hide those url from anyone that doesn't match a random url from database..meaning e.g
if user typed proposal.test.com/seo ,it shouldn't display the page, if the user
typed something like e.g proposal.test.com/seo/a13sdfa and a13sdfa matched a key from a databased, that's the only time the proposal.test.com/seo page will be displayed
so how am I gonna do this in PHP ? because all 3 directories are made up of pure static pages..
i have done the creating of keys already, i just wanna know how to hide these pages by appending a given random key and checking if it does or don't exists in database.
Since the pages are never considered PHP, you can not block the access using PHP.
You can block access by configuring your web server, for example by using a .htaccess file.
If you blocked access the normal way, you can use PHP to allow access to the files on certain conditions..
You should use mod_rewrite (in case of Apache web-server) and setup a rewriting of /a13sdfa into something like ?key=a13sdfa. Also you should include some PHP code in all static files in order to check the key validity.
How about this: move the static files outside the public folder, so they cannot be accessed directly; redirect all requests to a php file (you can use rewrite engine with apache) which will look in the database for the accessed url/key and return the file_get_contents of the corresponding file.
Here's an example of how the .htaccess file could look like:
RewriteEngine on
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^(.*)$ /index.php
What this does is the following: if the requested file doesn't exist on the disk (as a file or a directory), it will redirect to /index.php. There you should have the logic to render what page you want.
If you don't know in which variable the server will put the slug, just do a print_r($_SERVER) from inside index.php to find it.
There are 2 ways you could solve this problem.
1) (my prefered) Use .htaccess to only display the page if it matches the regex givin in the .htaccess.
2) In PHP (your actual question) 'Get the slug from the URL, query it to the database and if you get a result display it. Otherwise, send a 404 header from php.
Assuming the following: You have an Apache webserver with mod_rewrite enabled (check php info if you arent sure). Your virtual host allows overriding (AllowOveride All).
.htacces
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+) index.php?check=$1 [QSA,L,t]
If the file or directory exsists on the server it will display the page. So it would display seo, design etc. Otherwise it redirects to index.php and gives its slug as a parameter named $check. With this variable, query to the database, check te result and redirect to the desired page.