ReWrite Rule for Dbname as common subfolder - php

Could someone please help me with the following re-writes for Apache
I want the results to be:
domain.com/ (Landing search page for which DB username belongs to)
domain.com/db1/ (For index login page to db1 where db1 can be any other db name)
domain.com/db1/accounts/ (Once logged in)
domain.com/db1/admin/ (Another page once logged in)
Please help me with a url re-write if it is possible to do this...
I have something like this but it's not working:
`Options +FollowSymLinks
RewriteEngine on
RewriteRule /(.*)/$ /index.php?dbname=$1
RewriteRule /(.*)/account/$ /account.php?dbname=$1
`
Thank you
Regards
Johan

Avoid rewriting calls to /
Just present the login form in index or in whatever other page you want.
Then
This block tells the server to allow rewriting, to stick with existing files and directories and only rewrite what does not "exist", and that you will be rewriting from /
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteBase /
then your rules
RewriteRule ^db([0-9]+)$ index.php?dbname=$1&%{QUERY_STRING} [L]
RewriteRule ^db([0-9]+)/account$ index.php?admin=$1&%{QUERY_STRING} [L]
RewriteRule ^db([0-9]+)/admin$ index.php?admin=$1&%{QUERY_STRING} [L]
This is assuming that after login you will be sending him to
https://www.yoursite.com/db1
https://www.yoursite.com/db1/account
https://www.yoursite.com/db1/admin
for instance

Related

.htaccess Redirect any file in directory to index.php of same directory

I'm creating a blog, and would like to use the title of each entry as the URL.
The blog is pulling from a database and has code in a single php file to display the blog entry.
I would like any url like, domain.com/blog/this-is-the-title.html to redirect to
domain.com/blog/index.php
BUT, keep the URL in the browser bar as the original url.
EDIT...
domain.com/blog/anything-that-is-here.html
should redirect to domain.com/blog/index.php
But, still show domain.com/blog/anything-that-is-here.html in the browser address bar.
I hope this makes sense.
Hoping this is something that can be accomplished in.htaccess.
Thanks!
Rick
You are searching for the rewrite function from Apache.
Something like this should work for your case:
RewriteEngine on
RewriteBase /blog/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?title=$1
An URL like domain.com/blog/test-title will internally call your index.php with $_GET['title'] = "test-title".
Try using [P]
RewriteEngine on
RewriteRule ^(.*)$ / domain.com/blog/index.php [P]

.htaccess Search Engine & User Friendly URL's

Site Structure:
SITE ROOT
/Desktop/ - For everything seen at domain.com
/Mobile/ - For everything seen at m.domain.com
/Admin/ - For everything seen at admin.domain.com
.htaccess
RewriteEngine On
Options FollowSymLinks
RewriteRule ^([-_a-zA-Z0-9]*)/([-_a-zA-Z0-9]*)/([-_a-zA-Z0-9]*)$ $1.php?id=$2&name=$3
I've tried placing this at SITE ROOT and /Desktop/.
The Problem
I've set up a testing page domain.com/company.php which simply has <h1>Hello World</h1> within.
Upon going to: http://domain.com/company.php?company_id=74&name=DesignSourceLtd
"Hello World" displays
Upon going to: http://domain.com/company/74/DesignSourceLtd
A 404 page shows.
Question(s)
How do I make my URL search engine & user friendly and all preceding 'pages' of company show the company.php template?
Solution 1
You can handle all the URIs within index.php by creating .htaccess file like this (So you can treat any parameter as you want and other benefits ) :
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond &{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [QSA,NC,L]
And access parameters with explode('/',$_SERVER['REQUEST_URI']) and you can include company.php
Solution 2
You can create a spesific handle for company.php
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond &{REQUEST_FILENAME} !-d
RewriteRule ^company\/([-_a-zA-Z0-9]*)\/([-_a-zA-Z0-9]*)$ company.php?company_id=$1&name=$2[QSA,NC,L]
Solution 2 doesn't catch first url parameter because your GET Queries are spesific for company.php
Your options should be Options +FollowSymLinks - not sure if that will affect the outcome, however. Your forward slashes should also be escaped. Try regex101.com to test.
See https://regex101.com/r/PQpiLt/1

How can I use more than ONE vanity URL?

So I already have a vanity URL on my site for average users profiles...
basecentre.co.uk/user1
this redirects users to
basecentre.co.uk/userprofile.php?username=user1
using the code below...
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{QUERY_STRING} ^thankyou-page=([0-9]*)$
RewriteRule ^(.*)$ affilates_redirect.php [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /userprofile.php?username=$1
RewriteCond %{HTTP_HOST} ^www.basecentre.co.uk$ [NC]
RewriteRule ^(.*)$ http://basecentre.co.uk/$1 [R=301,L]
</IfModule>
I am trying to create business profiles too, and was looking to do the same sort of thing but using a different page on my site so say...
basecentre.co.uk/businessprofile.php?businessname=mcdonalds
would become
basecentre.co.uk/mcdonalds
And it wouldn't get confussed with the normal user profiles.
Would this work or can you only have ONE vanity URL on your site? If that is true, is there any other option I could use?
Also if this is possible, if you have a username with the same business profile name what would happen?
Thanks for the help!
I would recommend to use URL schemes like /user/user1 and /business/macdonalds. This will exclude any collisions.
You could also leave the user profile URLs like /user1, and add /business/macdonalds as a new scheme for business profiles.
Finally, it is not impossible to do what you like, but it is more difficult, because there are several constraints :
You must make sure that a user name never is the same as a business name.
You cannot redirect to either userprofile.php or businessprofile.php, but you have to redirect to a script (e.g. index.php) that does the routing.
You have to develop a router, that will lookup the users and business tables to find out which type of profile has to be shown, and finally execute the correct .php file.
You could do something such as:
RewriteEngine On
RewriteRule ^page/([^/]*)$ /businessprofile.php?businessname=$1 [L]
RewriteRule ^user/([^/]*)$ /userprofile.php?username=$1 [L]
Which would redirect a user profile for example to basecentre.co.uk/user/... and a business profile to basecentre.co.uk/page/...

htaccess Redirect Causes Errors

I'm working on a website that has been built sloppily.
The website is filled with regular links that are translated into the corresponding .php pages by the .htaccess page.
This is it:
RewriteEngine on
RewriteRule ^koral/(.*)/$ page.php?name=$1
RewriteRule ^koral/(.*)$ page.php?name=$1
RewriteRule ^(.*).html/(.*)/(.*)/(.*)$ cat.php?cat=$1&page=$2&order=$3&dir=$4
RewriteRule ^(.*).html$ cat.php?cat=$1
RewriteRule ^(.*)/(.*).html$ product.php?cat=$1&product=$2
<IfModule mod_security.c>
SecFilterEngine Off
</IfModule>
First of all, I would love some help regarding whether or not this page has everything it should. I've never messed with it before.
Secondly and my main issue, if, for example, I would write the address www.thewebsite.com/foobar.html, it would be translated into www.thewebsite.com/cat.php?cat=foobar by the .htaccess page, and it would give a database error (and reveal information about the database).
I've put a check into cat.php which checks if the category exists, but I can't redirect the user to the 404 error page. There's a page called 404.shtml in the website, but redirecting the user to it causes the .htaccess to just change it again to cat.php?cat=404.
Is the way they used the .htaccess page normal? Should I change this system?
And how are users sent to error pages? From what I understood the server should be doing it on its own?
I would love some clarification... There is some much about this subject I don't understand.
Update:
This is my new .htaccess page
RewriteEngine on
RewriteRule ^error.php?err=(.*)$ Error$1.html
# Only apply this rule if we're not requesting a file...
RewriteCond %{REQUEST_FILENAME} !-f [NC]
# ...and if we're not requesting a directory.
RewriteCond %{REQUEST_FILENAME} !-d [NC]
RewriteRule ^koral/(.*)/$ page.php?name=$1
RewriteRule ^koral/(.*)$ page.php?name=$1
RewriteRule ^(.*).html/(.*)/(.*)/(.*)$ cat.php?cat=$1&page=$2&order=$3&dir=$4
RewriteRule ^(.*).html$ cat.php?cat=$1
RewriteRule ^(.*)/(.*).html$ product.php?cat=$1&product=$2
<IfModule mod_security.c>
SecFilterEngine Off
</IfModule>
Because the redirecting is in the code and the user cannot see it, I allowed myself to write the link in a non-clean way. I tried turning it into a clean URL but the following does not do anything:
RewriteRule ^error.php?err=(.*)$ Error$1.html
Can someone please help me understand why? I thought since error.php is a real page, I should put it before the conditional but it didn't work. BTW, I saw in an article about .htaccess that the page should start with Options +FollowSymLinks. It seems to me that everyone sort of has their own way of writing it. Is there a guide or something like that, which I can be sure is authentic and covers all the bases there is about .htaccess?
Thank you so much!!
Using rewrite rules to work around links to .html pages that don't exist is unusual in my experience, but it's really just a different take on "pretty" URLs, e.g. www.thewebsite.com/foobar/ gets routed to cat.php?cat=foobar on the backend.
Your 404 issue is different. You need to be able to display error pages.
One option here is to rewrite requests as long as they don't request an existing file. This is very common for serving up static content like images, CSS files, and the like. To do this, you can use the -d and -f options to RewriteCond, which apply when requesting a directory and file respectively:
RewriteEngine On
# Only apply this rule if we're not requesting a file...
RewriteCond %{REQUEST_FILENAME} !-f [NC]
# ...and if we're not requesting a directory.
RewriteCond %{REQUEST_FILENAME} !-d [NC]
RewriteRule ^([^.]+)\.html$ cat.php?cat=$1 [L,QSA]
Now, requests to 404.shtml should go through, because you're requesting an existing file on the filesystem.
Note that the RewriteConds only apply to the single RewriteRule that immediately follows. For additional RewriteRules, also include additional RewriteConds.
Your regex is wrong anywhere. Literal dot needs to be escaped using otherwise it will match any character. Also it is better to use L and QSA flags to end each rule properly.
RewriteEngine on
RewriteBase /
RewriteRule ^koral/([^/]+)/?$ page.php?name=$1 [L,QSA]
RewriteRule ^([^.]+)\.html/([^/]+)/([^/]+)/([^/]*)/?$ cat.php?cat=$1&page=$2&order=$3&dir=$4 [L,QSA]
RewriteRule ^([^.]+)\.html$ cat.php?cat=$1 [L,QSA]
RewriteRule ^([^/]+)/([^.]+)\.html$ product.php?cat=$1&product=$2 [L,QSA]

.htaccess to redirect parts of a URL to local files

I have a custom web app and the file structure works like this:
/apps/calendar/frontend/index.php
/apps/calendar/frontend/view/index.php
/apps/calendar/backend/index.php
/apps/calendar/backend/edit/index.php
/apps/calendar/backend/add/index.php
/apps/calendar/backend/view/index.php
I'm trying to write a .htaccess file to help redirect the files so they cant see the 'real' path.
RewriteEngine on
RewriteCond %{REQUEST_URI} !^/admin
RewriteRule ^([^/\.]+)/(.*)/(.*)($|/$) /apps/$1/frontend/$2/$3 [NC,L]
RewriteRule ^([^/\.]+)/(.*)($|/$) /apps/$1/frontend/$2 [NC,L]
RewriteRule ^([^/\.]+)($|/$) /apps/$1/frontend/ [NC,L]
When I visit localhost/calendar it should map redirect to /apps/calendar/frontend/index.php. But when I visit localhost/calendar/add it gives me a 301 (permanent move) then shows the full page of localhost/apps/calendar/frontend/add/index.php in the console. Anyone got any ideas why this would happen? Or a better way around this? The apps might have heaps of sub-directories so, I'm not particularly keen on having a rule for ever subdirectory combination.
As you can see also I have a /admin path, which would load the /backend/ parts of the app. I would assuming I can do the similar code with the prefix of /admin?
This question might also be of your interest: Create blog post links similar to a folder structure.
Given that your .htaccess is located on the root folder of your domain /home/youraccount/public_html/.htaccess, it would look like this:
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} !^/(admin|apps) [NC]
RewriteCond %{DOCUMENT_ROOT}/apps/$1 -d
RewriteRule ^([^/]+)/?(|.*)$ /apps/$1/frontend/$2 [NC,L]
RewriteCond %{REQUEST_URI} !^/apps [NC]
RewriteCond %{DOCUMENT_ROOT}/apps/$1 -d
RewriteRule ^admin/([^/]+)/?(|.*)$ /apps/$1/backend/$2 [NC,L]
Let's say the user access:
http://domain.com/calendar
http://domain.com/calendar/
http://domain.com/calendar/add
All the above would redirect to
/apps/calendar/frontend/index.php
/apps/calendar/frontend/index.php/
/apps/calendar/frontend/index.php/add
And the if the user access:
http://domain.com/calendar/admin
http://domain.com/calendar/admin/
http://domain.com/calendar/admin/add
It would go to:
/apps/calendar/backend/index.php
/apps/calendar/backend/index.php/
/apps/calendar/backend/index.php/add
So it would make index.php your controller for each end:
/apps/calendar/frontend/index.php
/apps/calendar/backend/index.php

Categories