Mod_rewrite and complex redirection technique recomendations - php

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 .

Related

PHP API main file

I have a question concerning an API that I am developing and I have just started asking myself the following question: As a rule, to reach the main file, you have to reach it in the following way api.domain.com/index.php/users/id/profile/... but many sites allow you to avoid calling the file index.php but put the "query" directly. My question is, what should I do in order not to enter the file name, since if I don't put index.php it goes missing because it sees the query as folders
You could use .htaccess file.
As an example, you may add this to your .htaccess:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php [L,QSA]
And then use some sorte of routing (like FastRoute or Laravel routes) to redirect the user where you wanted. This way, you could call api.yousite.com/users/id/... and it'd go through the index.php anyway.
You can also take a look at this or this.

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

Redirect example.com/out/1234 to another URL

I know theres a lot of posts about redirects but this is a little different (I think).
Basically I want my outlinks to be example.com/out/1234 and I want them to go to a php that looks up the URL 1234 if referenced to in MySQL and the php header redirect to that URL.
The problem Im having is passing 1234 to a page. I know how if it was out.php?q=1234 but I want it to be /out/1234
Does there need to be an index file within an /out directory that also has a htaccess to rewrite it?
If so, any ideas what the regex need to be to do this? I have seen a few sites doing this and I cant work it out.
htaccess file in your document root, you can try adding:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/?out/(.*)$ /out.php?q=$1 [L]
Replace the /out.php with whereever your php script for handling the URL is

What's Wrong? Nothing Will Load (Almost)!

Okay...My title is a bit of an exaggeration...
My site is built in PHP, and all the files I'm trying to "require_once" aren't being loaded. The only file I've changed is my .htaccess file. I don't know a thing about .htaccess and what I have is purely from searching the web. What is wrong? Thanks for any help in advance.
RewriteEngine on
<Files 403.shtml>
order allow,deny
allow from all
</Files>
ReWriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
ReWriteRule !index.php index.php [L]
Also, if I comment out the bottom two lines, my site works great.
Well, require_once has nothing to do with .htaccess file: it's a PHP directive, not an Apache one. You have to set correctly the include_path for your files and make sure these directories and files are reachable (i.e., with correct privileges set on them).
If you show the error message you got from failed require, it'd be much more simple to give you a specific advice on how to fix it.
UPDATE If what you need is redirecting all the non-AJAX requests for .php files into index.php, your .htaccess should like this:
RewriteEngine on
RewriteCond %{HTTP:x-requested-with} ^XMLHttpRequest$
RewriteRule . - [L]
RewriteCond %{REQUEST_FILENAME} !-d
ReWriteRule .php$ index.php
This basically means the following: "all AJAX requests - go for what you need, all non-AJAX requests IF you're not going for some directory and are ended with .php - go for index.php instead".
Without checking for .php (or some similar check) you will redirect to index.php all the script loading procedures; and, unless you do it from some external CDN, it's not what would work in your case. )
Try changing the last two lines to this:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule . index.php [L]
If you want your URL's to look something like this (you probably do):
http://yoursite.com/some/path/somewhere
then change the last line to:
RewriteRule ^([^/]*)(.*)$ index.php?first=$1&second=$2
If that's what you want to achieve, ensure that if you're trying to go to:
http://yoursite.com/about
That there isn't actually a folder called about, this line:
RewriteCond %{REQUEST_FILENAME} !-d
Checks to see if a folder with the name "about" exists, if it does, then the page will not redirect, the same goes for files, say you go to:
http://yoursite.com/about.html
If about.html actually exists then the page will not redirect.
Hope that makes sense.
If you need more information, http://jrgns.net/content/redirect_request_to_index seems to be fairly succinct and helpful.

Mod_Rewrite Query with Root Domain

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]

Categories