Define root in .htaccess - php

I am trying to make clean url in a website using RewriteRule . But when i call the page from different pages (eg. Header-included in different pages) , it shows 404 - object not found.
I observed that the url treated as different in each page. As per my observation i may need to set a root folder and have to redirect all pages based on that . So that it will redirect to the right destination.
Any information on .htaccess will be helpful
my .htaccess code as follows:
DirectoryIndex index2.php
Options +FollowSymlinks
RewriteEngine on
RewriteRule ^(.*)/([a-zA-Z0-9]+)/songs?$ album.php?ad=$2 [QSA,NC]
RewriteRule ^category/([a-zA-Z0-9]+)?$ category.php?ct=$1 [QSA,NC]
RewriteRule ^(.*)/([a-zA-Z0-9]+)/cat?$ /category.php?ct=$2 [QSA,NC]
and my directory:
localhost
|
+mysite
|
+mp3
|
index2.php
category.php
album.php

Related

New site is loading Under Construction page instead of Wordpress Site

I created my first online site. I put all my files on the server (I used Wordpress for now) and it's great, but, when I enter my site address (let's say: www.mysite.com) I see only "Under construction". I only see my website after typing: www.mysite.com/index.php.
How can I make www.mysite.com load index.php without needing to enter it in the address bar?
There is probably an index.html file at the root of your public html folder that you need to delete. Once you delete this, your site will load as normal.
Most web servers load .html files before any other files. So if you have index.html and index.php in the root of your public html folder, it will most likely load index.html first.
You also may need to make sure you have an .htaccess file in the root of your public html folder, and that it contains the basic Wordpress code:
# 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
More info on that here.

htaccess redirection rules with conditions

I used the following code in my htaccess
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php
</IfModule>
It redirects as if the url is test.com/test --> it redirects to test folder (Here the folder is available in the name of 'test')
If the url is test.com/testpage --> it redircts to index.php (i.e no one folder is in the name of testpage)
But i want to redirect this as
if the url is test.com --> it will redircts to index.php
if the url is test.com/test --> It will redirects to corresponding folder (if the folder name exists)
if the url is test.com/testpage then it redirects to page.php
Also i want to redirect some particular name to test.php (test.com/test2 or test.com/test3)
Please help this
This is done by creating a router class.
The code you have there redirects all urls to index.php making index be able to read the url and base it's contect on that.
see http://www.phpaddiction.com/tags/axial/url-routing-with-php-part-one/
for router tutorial
you can individually write their rewrite conditions for specific pages/urls or just add some sort of suffix like along with page pass "[category]/testpage" or any thing and describe the rewrite condition so that it can be redirected.
RewriteRule ^category/(.*)$ page.php [R=301]

URL rewrite htaccess

I have a small website where I have wrote some rules
RewriteEngine On
RewriteRule ^[!/.]*([A-Za-z0-9]+)/?$ /index.php?id=$1 [NC,L]
This redirects all of my links to example.com/index.php. Whereas I want them to redirect all the URLs with one directory example.com/dir to be redirected to this link example.com/index.php, and URLs with two directories, example.com/dir1/dir2 should redirect to example.com/index2.php, and rest should not be redirected.
you can use the below code .. i'll explain it a little ..
http://www.example.com/customdir/index/1/
customdir = Your Sub Directory
index = index.php
1 = Your id
Options +FollowSymLinks
RewriteEngine on
RewriteRule customdir/(\w+)/?(\d+)?/? customdir/$1.php?id=$2

htaccess Challenge: rewrite perfection for secure website structure

I am working on a new project where I want to improve the security by using 2 level folders.
The folder "Core" contains all the folders including the "public" which needs to be the only folder the visitor can access.
A short introduction about my webdir:
webdir
| - .htaccess (core)
| - classes
| - functions
| - public
| - .htaccess (public)
| - css
| - files
| - de
| - en
| - general
| - nl
| - images
| - index.php
| - js
| - swf
| - template
For your information:
My public/index.php file sorts out in which section the files are located via [geturl] and [section]. The request "http://example.com/nl/openfile" points to:
public/index.php
this file reads the $_GET and sees that [geturl] is "openfile" in the [section] "nl"
public/index.php wants to open: "files/nl/openfile.php"
this is how it works and I want it to work!
What I want to achieve with my .htaccess is the following:
The website supports 3 languages: en/de/nl. When no language is set then the nl language should be default.
http://example.com/nl/contact needs to target the file: public/files/nl/contact.php
Currently, when the visitor inputs http://example.com/nl/css (with no trailing slash, the problem doesn't occur when the trailing slash is added) the server translates this to http://example.com/public/css/?section=nl.
This is too much information for the visitor. I don't want users to see where the files are located.
The public folder should not be accessed directly. So when a users inputs http://example.com/public this should be redirected to http://example.com/nl/
My current .htaccess files:
.htaccess (core)
<IfModule mod_rewrite.c>
RewriteEngine on
Options +FollowSymLinks
Options -Indexes
RewriteCond %{HTTP_HOST} ^customer.com$ [NC]
RewriteRule ^(.*)$ http://www.customer.com/$1 [R=301,L]
RewriteRule ^company(.*)$ public/$1?section=company [L]
RewriteCond %{QUERY_STRING} (.*)
RewriteRule !(nl|en|de)/(.*).* /webdir/nl/ [R=301,L] # IF NO LANG ISSET THAN NL REDIRECT
RewriteRule ^error(.*).* public/?getpage=error&%1 [PT,L]
RewriteRule ^(nl|en|de)/(.*).* public/$2?section=$1&%1 [L]
</IfModule>
.htaccess (public)
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{QUERY_STRING} (.*)
RewriteRule ^(.*).* index.php?geturl=$1&%1 [PT,L]
</IfModule>
What am I doing wrong and how can I improve the .htaccess files?
Summarized, I want the .htaccess to do the following:
The website supports 3 languages: en/de/nl. When no language is set then the nl language should be default.
http://example.com/nl/contact needs to target the file: public/files/nl/contact.php
Currently, when the visitor inputs http://example.com/nl/css (with no trailing slash, the problem doesn't occur when the trailing slash is added) the server translates this to http://example.com/public/css/?section=nl.
This is too much information for the visitor. I don't want users to see where the files are located.
The public folder should not be accessed directly. So when a users inputs http://example.com/public this should be redirected to http://example.com/nl/
Do you know you can block an entire directory by putting an .htaccess with deny from all inside?
This way you can keep your project structure simpler and more portable.

.htaccess in subdirectory

I've been searching the whole evening for a solution/approach for my problem with my .htaccess file.
Basically I have www.site.com with a .htaccess in the www directory with the following content:
Options +FollowSymlinks
RewriteEngine on
RewriteCond %{REQUEST_URI} !^(/admin)
RewriteRule ^[^_+/]. index.php
It works just fine and allows www.site.com/en/articles/Interesting_title/3 to be parsed by index.php which reads which controller, language and what article to display.
However I'd like for the admin system to work the same way. www.site.com/admin should have an .htaccess files that allow me to write URL's this way.
www.site.com/admin/en/articles/article_title/3 should allow me to edit article number 3 using en english UI.
Dumping $_SERVER['REQUEST_URI'] in the first case gives "en/articles/Interesting_title/3"
Dumping $_SERVER['REQUEST_URI'] in the second case gives "admin/en/articles/article_title/3"
If I at a later point choose to move administration to www.site.com/shassooo I would like to avoid changing any code other then the .htaccess files.
Cheers
Append this line in the same .htaccess file you have, not under admin sub directory:
RewriteCond %{REQUEST_URI} !^/admin/index.php$ [NC]
RewriteRule ^admin/[^_+/]. /admin/index.php [L,NC]

Categories