Mod_Rewrite Query with Root Domain - php

I'm trying to do a query on some information if someone types, for example:
http://domain.com/abracadabra
If someone enters in this address, I want it to go to:
search.php?query=abracadabra
I've done rewrites with queries before, but not from the root.
Any help would be great!
Would it be possible to check if the string being searched is numerical and send it to a different PHP page? Maybe link to a directory with a new .htaccess?

Doing it from the root is no different than doing it anywhere else. The below will turn every request for a resource (that doesn't exist in the file system) to search.php.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ search.php?query=$1 [L,QSA]

Related

How to send data to a PHP file and point to what it returns through htaccess

I have set up my Virtual Host in a way that it points all subdomains to their respective directories. For example, ok.domain.com points to domain.com/ok.
ok.domain.com/page points to domain.com/ok/page.
However, I want users to be able to have their own custom domains point to their directories.
I have a PHP file containing keys that point a user's custom domain to whichever directory belongs to them.
"someusersite.com"=>"bob",
"anotheruserdomain.co"=>"test"
Is there a rewrite rule I could use that sends the URL the user is on to this PHP file and then points to whatever it returns.
So if the user is on someusersite.com, this domain should be sent to the PHP file which in turn will echo out the value bob. Now the user should be pointed to bob.
Each of these custom domains will have A Records put in place.
I have tried letting users point their domain to their subdomain via a CNAME record but that doesn't work - it just points to the root site.
Thank you!
Redirect all to index.php htaccess
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?path=$1 [NC,L,QSA]
Redirect all access from a folder to a PHP file
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^doc\/(.*)\.html$ download.php?r=$1 [L,R=301]

RewriteRule different pages per user

I am deciding to create separate profile links for each user who registers on the website. I am using the .htaccess file RewriteRule to achieve this. The url I want to achieve should look something like www.domain.com/username. (I don't want www.domain.com/users/username)
Now the problem is, if I add a rule like RewriteRule ^(.*)$ /users.php?username=$1
it will matchup all URL addresses for www.domain.com, which may direct to any path. Even if I would like to visit www.domain.com/about page, it will be redirected to
www.domain.com/users.php?username=about, which I don't want. (Even if the requests was www.domain.com/products/abc)
I could apply filters in my users.php to filter such usernames, or if a username is not found in database, redirect to the page, but this means I have to change filters every time I decide to add a new page in my directory (in this case, any). Also, all traffic will be directed through 1 page, which may cause a performance problem.
Please suggest a way to achieve this, as There are websites that work like this. A possible RewriteRule or PHP code to achieve this.
You can use this rule in your root .htaccess:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(\w+)/?$ /users.php?username=$1 [L,QSA]
I always use just simple rewrite as below:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*)(.*)/?$ index.php
All traffic is redirected to index.php and using php I can run specific controllers depending on url. You should also think about. Almost all frameworks use such rule.
Then in your PHP file you should examine
$_SERVER['REQUEST_URI']
variable to route request to specific controllers

How to create a directory with a single file in it?

I'll be signing businesses up to advertise on my website, and I want them to have a direct URL for their customers to go to.
Like, instead of www.website.com/page.php?id=324234234,
I want to have www.website.com/businessname
Is there a simple way to do this? I've searched and seen a whole bunch of different things people are trying to do but I haven't seen anything that's the same as what I want to do.
I'm using a VPS, and I want to make sure that I don't open up permissions so that anyone can get in there and mess things up.
Also, these users will not be signing themselves up. I will be doing that.
The simplest way to get my end result is what I'm looking for. Thanks!
Basic URL rewriting could work.
Add to your .htaccess file
RewriteEngine on
RewriteRule ^(.*)$ page.php?businessname=$1 [L]
Then use PHP to rewrite the businessname to the ID of the company / find the data.
Of course .htaccess rewrite rules is a complete science if you need more complex rewriting...
Re-iterating what jtheman said with a little more explanation:
Create a file named .htaccess with the contents:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ page.php?businessname=$1 [L]
You need, of course, the ability to have directory level .htaccess enabled - you're using a VPS so you should be able to do this if it is not already enabled.
So let me explain what each line will do.
RewriteEngine on
Turns on the ability to URL re-write
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
Tells Apache not to re-direct files that exist in the directory already
RewriteRule ^(.*)$ page.php?businessname=$1 [L]
This is where the magic happens.
^(.*)$ this part is like a regular expression match. It will tell Apache to collect any URLs that have any characters within them and redirect them to page.php?businessname=(.*)
So, if you post:
www.website.com/stackover
It will really be sending: www.website.com/page.php?businessname=stackover
Then you can just use $_GET[businessname] to dynamically update the page.
Hope this helps!

Apache presenting virtual subdirectories and passing parameters to PHP script

My root directory contains a selection of PHP files: index.php, about.php etc. Each of these can take an ID parameter to customize a few of the variables on the page, such as contact phone number, email address etc.
If a visitor was to access http://myserver/joebloggs (joebloggs being a parameter, not a subdirectory), I want them to be served up with the index.php page from the root directory, but for joebloggs to be passed to index.php as the ID and the page then customized. I don't want to have to create a subdirectory for every user ID, I'd rather maintain this info in a DB and have PHP generate the pages for me.
Furthermore, when the user then navigates to the about.php page, I would like this ID to be carried over and then have the about page customized too, i.e. a link to 'about' maintains the ID parameter in the URL: http://myserver/joebloggs/about/
I've got a rough idea in my mind how to do this and have been reading up on mod_rewrite but haven't had much luck in piecing various solutions together.
Any pointers or help would be much appreciated.
Try something like this:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} \/(.+?)\/about
RewriteRule . /about.php?p1=%1 [L]
RewriteCond %{REQUEST_URI} \/.*?
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*?)$ /index.php?p1=$1 [L]
Should do the job. Tested it on my server.

Mod_rewrite and complex redirection technique recomendations

im trying to solve this issue. I have a web site with this simplified struture:
mysite.com/index.php
mysite.com/faq.php
mysite.com/url.php
mysite.com/users/some_content_here
If the user call the index page or the faq page they have to arrive to that pages. BUT if the user writes something like mysite.com/xgsfd (or any other string different than index or faq) they have to call the url.php wich recive the xgsfd string via GET and redirects the user to a particular page.
The url.php script is already done, but i have no idea how to solve the other part, im was thinking using a .htaccess file in the root directory but as you can see it will trigger a infinite loop of rederictions.
Any idea how to solve this issue? Thanks for any help!
You can use rewrite conditions to serve files, folders and symlinks if they actually exist, but otherwise rewrite to the url.php:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-s
RewriteCond %{REQUEST_FILENAME} !-l
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ url.php?url=$1 [QSA,L]
That way requests to faq.php or index.php will be served, while a request to mysite.com/xgsfd will end up rewritten to url.php?url=/xgsfd .

Categories