.htaccess Search Engine & User Friendly URL's - php

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

Related

ReWrite Rule for Dbname as common subfolder

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

Wordpress after migration doesn't work

I had problems with WordPress migration from one server to another, and generally now it works but I have many smaller problems..
Before I use domain oldexample.com and now i use domain newexample.com/something . Generally everything on the page works but changing language isn't work (qTranslate plugin).
I think that I found the reason of this - in admin menu I found in some places situation that href links start from "/" for example: "/wp-admin/..." and in result it change the URL from newexample.com/something/wp-admin to newexample.com/wp-admin. I see this problem in qTranslate settings links and when I want delete some plugins. I get the error message "404 Not Found - nginx/1.4.5" in results...
Did you see this problem before? Maybe I should change something in WordPress core files? .htaccess? Now it looks like this:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www\.)?newexample\.com\/something$ [NC]
</IfModule>
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
After long time I find the solution! All "href" are generated by using $_SERVER['REQUEST_URI'], and this variable show only the rest part of the path (without /something/. I add to wp-settings.php this part of code: $_SERVER['REQUEST_URI'] = '/something'.$_SERVER['REQUEST_URI']; (on the very beggining) all forms generate good path.

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]

Can't get mod_rewrite to work

I am trying to get url from:
192.168.0.1/movie-page.php?id=123
to:
192.168.0.1/movie/movie-name
or even (for now):
192.168.0.1/movie/123
I've simplified it by using this url (to get something working):
192.168.0.1/pet_care_info_07_07_2008.php TO 192.168.0.1/pet-care/
my .htaccess file:
RewriteEngine On
RewriteRule ^pet-care/?$ pet_care_info_07_07_2008.php [NC,L]
What am I doing wrong? I've tried many combinations but no luck and my patience is running out...
I am running this on my local NAS which should have mod_rewrite enabled by default. I have tested .htaccess by entering random string in .htaccess file and opening the page, I got 404 error. I assume this means that .htaccess is being used since the page stops functioning if the file is malformed.
If you want to rewrite:
192.168.0.1/movie-page.php?id=123 too
192.168.0.1/movie/movie-name or 192.168.0.1/movie/123
Then you would do something like, but will require you manually add a rewrite for any new route (fancy url) you want, and eventually you may want your script to create routes dynamically or have a single entry point to sanitize:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^movie/([a-zA-Z0-9-]+)$ movie-page.php?id=$1 [L]
So a better method is to route everything through the rewrite:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?route=$1 [L,QSA]
Then handle the route by splitting the $_GET['route'] with explode()
<?php
//192.168.0.1/movie/movie-name
$route = (isset($_GET['route'])?explode('/',$_GET['route']):null);
if(!empty($route)){
//example
$route[0]; //movie
$route[1]; //movie-name
}
?>
You want something like this:
RewriteRule ^movie/([0-9]*)$ /movie-page.php?id=$1 [L,R=301]
That will give the movie ID version with a numeric ID.

Strange situation mod_rewrite and PHP

I do not understand something ...
I have CMS written by me and have some strange problem about hidden error from "mod_rewrite". In apache2 error.log "mod_rewrite" giving :
File does not exist: /home/path/to/request, referer: http://IP/request?view=1
my $_GET / $_POST request are all empty
$_SERVER['REQUEST_URI'] is ok (returns request?view=1)
$_SERVER['QUERY_STRING'] is also empty
also i install Drupal CMS to test is it problem on web server, but Drupal works just fine... then i copied drupal ".htaccess" file to my CMS in order to fix mod_rewrite preferences. (Drupal do not create apache error like my )
No luck, i have same problem again.
also tried with all options in .htaccess from Drupal CMS
I try to turn on RewriteLog (several attempts) but without result, not a single log file was not created.
Am i something missing or ... just my cms sucks
and one more thing, my CMS work just fine on other webserver with exactly the same files .... ?????
RewriteEngine On
Options -Indexes
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !=/favicon.ico
#tested RewriteRule ^ index.php [L]
#tested RewriteRule ^ index.php [L,QSA]
#tested Options FollowSymLinks
#tested Options -MultiViews
#tested RewriteBase /
P.S. sorry for my bad English
Not sure if that's your whole .htaccess but you need to enable rewrite, here's a rewrite I use and its never failed:
RewriteEngine On
Options -Indexes
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?route=$1 [L,QSA]
The RewriteRule passes everything (.*) to a route parameter in index.php then my router class handles the controller ect, the QSA flag will also allow me to pass extra GET/POST values to any part of the script.
hope it helps
ps I dont add the RewriteCond %{REQUEST_FILENAME} !-d because I dont allow folder views and I dont serve content from a folder, everything goes through a controller inc (images,css,js), so there is no need for this in most cases. And it also protects the folders like core & template ect ;)
finally I found the solution of my problem!
The answer is very,very,very simple...
In apache2 configuration (on Ubuntu) I found the <Directory tag is duplicated by default ... I just erase all directories tags and create one basic.
reload apache2 configuration and every thing starts working just fine

Categories