PHP index.php File get other pages content from subdirectory location - php

How to get only an index.php in the server root directory and have all linked pages like /about.php /contact.php and so on located in a subdirectory like /pages/dist in the server? If someone visits example.com/about.php it get's the content from /pages/dist/[filename.php] ?

There are two ways that I have found to do it.
Using htaccess with regex:
Using rewrite engine, you link the base url to a folder like this:
RewriteEngine On
RewriteRule ^(?!index\.php)([\w-_]+)$ pages/dist/$1.php [QSA,L]
RewriteRule ^(?!index\.php)([\w-_]+\.php)$ pages/dist/$1 [QSA,L]
The second line is for files that end without the .php tag, and the third with.
What this does is if the user inputs in the url www.example.com/test.php it will show what is on the www.example.com/page/dist/test.php, even if the user strips the php tag, but if he inputs the index.php it will stay the same.
Using htaccess without regex:
This one is just for the simplicity, but if we where not to use regex it could look like this:
RewriteEngine ON
RewriteRule "test.php" "page/dist/test.php"
RewriteRule "test2.php" "page/dist/test2.php"
RewriteRule "test3.php" "page/dist/test3.php"
...
Please correct me if I'm wrong.

Related

How do I get mod-rewrite to access the proper directory?

I'm trying to use mod-rewrite to eliminate urls like {root}/index.php?action=about&page=faq. I want {root}/about/faq to redirect to the php url. My .htaccess file looks like
DirectoryIndex index.php
RewriteEngine On
RewriteRule ^(index|about|calendar|news|contact)$ index.php?action=$1 [L]
RewriteRule ^(index|about)/(.+)$ index.php?action=$1&page=$2 [L]
When redirecting to index.php?action=$1, this works fine. However, it doesn't work completely when the second rule is used. The html content appears fine, but the stylesheet and images are missing. The page seems to think that it's in a subdirectory. So {root}/about/faq looks for images in the folder about/images, instead of in the images folder found at the root. The PHP script says that the working directory is still the root directory, however. How do I make the pages think that they are in the root directory? Thanks!
RewriteRule ^(index|about|calendar|news|contact)/(.*)/$ /index.php?action=$1&page=$2
Would result in /about/faq/ rewriting to index.php?action=about&page=faq
/calendar/month/ would result in index.php?action=calendar&page=month
Also unless you want to rewrite imagesfiles I would add:
RewriteCond %{REQUEST_URI} !(\.gif|\.jpg|\.png)$ [NC]
This will stop image requests being rewritten.

htaccess need help rewriting url

I've been trying to rewrite my URL's with a htaccess file.
My project is using my index.php as it's basic controller.
I fetch the pages from the database with the get-value "naam".
Example:
localhost/YourDesigns/YDCMS/index.php?naam=home (this shows the homepage)
localhost/YourDesigns/YDCMS/index.php?naam=about (this shows the about page)
Now i want to rewrite the URLS to be shown like this:
localhost/YourDesigns/YDCMS/home (this shows the homepage)
localhost/YourDesigns/YDCMS/about (this shows the about page)
I have done some htaccess stuff myself and i've successfully removed the "index.php" from the url, but the "naam" still remains.
How do i remove this?
This is my current htaccess file:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /YourDesigns/YDCMS/index.php?naam=/$1 [L]
Thanks in advance :)
I think your way to see the process it's not totally right, if you want to show the url like you say localhost/YourDesigns/YDCMS/home you have to first replace the url inside your html content to that format, then by using a RewriteRule you call the correct path internally:
RewriteRule ^desired_url_path/([a-z0-9\-]+)$ /base_url_path/index.php?naam=$1 [L]
this way when an user click on a link the Apache server will use the above regex to convert the url by a rule that basically says : everything after the base url is aparameter value to be passed on the index.php.
Naturally the regex can be modified to suit your needs, the one i've written above it's a basic string pattern.

Remove in between folder structure from the url in a php website

I have a php website having following folder structure (basic structure).
project_name
app
controller
model
view
css
js
img
index.php
So when I view index.php in WAMP the url is http://localhost/project_name/
But when I go inside the site (eg. login.php which resides under view folder) url is like this. http://localhost/project_name/app/view/login.php
I found that using .htaccess we can change the urls. So I tried this (in .htaccess).
RewriteEngine on
RewriteBase /
Redirect 301 /project_name/app/view/login.php /project_name/login.php
RewriteRule ^/project_name/login.php$ /project_name/app/view/login.php [L]
Now url is http://localhost/project_name/login.php It is correct. But it seems php does not use the original link to grab the file (ie. from /project_name/app/view/login.php) but from here /project_name/login.php
So it throws 404 error.
What should I change? Please help me, i am just trying to hide /app/view/ part from the url so that user won't see my folder structure. I have read about various ways of doing that for about 9hrs today but still couldn't get anything working correctly.
Hope my question is clear enough. Any help is greatly appreciated!
URI's passed through rewrite rules in an htaccess file has the leading slash removed, so your rule:
RewriteRule ^/project_name/login.php$ /project_name/app/view/login.php [L]
won't ever match anything because of ^/. Also, since you are rewriting to a URI that matches a previous redirect, your browser will see a redirect loop and say that it's not redirecting properly. You'll need to match against the actual request and not the URI:
RewriteEngine on
RewriteBase /
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /project_name/app/view/login\.php
RewriteRule ^ /project_name/login.php [L,R=301]
RewriteRule ^/?project_name/login.php$ /project_name/app/view/login.php [L]

How to shorten urls in codeigniter using .htaccess?

I want to know if there is any way to shorten urls in a codeigniter using .htaccess. Like for example I have example.com/user/account/settings, I would like to shorten the url to example.com/settings. In the same way we remove index.php from the address bar, is there a way to remove more items from the same?
It's done by routes.php file in your CodeIgniter application folder. Documentation.
For your example it would be really easy, just add a line to the file:
$route['settings'] = 'user/account/settings';
By default, the index.php file will be included in your URLs: e.g
example.com/index.php/news/article/my_article
You can easily remove this file by using a .htaccess file with some simple rules. Here is an example of such a file, using the "negative" method in which everything is redirected except the specified items:
RewriteEngine on
RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]
In the above example, any HTTP request other than those for index.php, images, and robots.txt is treated as a request for your index.php file.

htaccess mod_rewrite help

i have a domain with a website in a folder.
i.e
http://domain.com.au/website/page.php
i only have access to .htaccess files to make the rules.
i would like my url to look like this..
http://domain.com.au/page
so in essence i want to drop the subfolder its sitting in, and the php extention.
All links in my php are structured like this though
page link
this is the same for js and css. they are all referenced from the 'website' folder.
will the rewrite mean i have to change all my links?
priority is dropping the folder from the url, not so much the php extension.
1) You could move all the files to the root web directory and run the website from there.
2) You would have to manually modify every link to remove the website directory from the beginning and then use the .htaccess file to redirect the pages back to the appropriate page. If you're going to do it this way, you might as well remove the PHP extension...
RewriteEngine On
Options +FollowSymLinks
RewriteCond %{HTTP_HOST} ^domain.com.au$ [NC]
RewriteRule ^(.*)/ website/$1.php [L]
You may have to modify the website/$1.php part to include your entire root directory in some cases, but that should work as long as the .htaccess file is in the root web directory. That will redirect any page in the form 'http://domain.com.au/page' to 'http://domain.com.au/website/page.php' while keeping the URL the same in the address bar.
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^page\_one/(.*)/([0-9]{1,6})/?$ webroot/products/products/view/$2 [L]
</IfModule>
this htaccess code redirects all trafic from www.domain.com/page_one/anything/numeral
to the page www.domain.com/webroot/products/products/view/same numeral
for your example, something like
RewriteRule ^page?$ website/page.php$2 [L]
note, this works for "page" as a string, if you need other functions , change it
for more insight, you might want to take a look at this link :
http://www.javascriptkit.com/howto/htaccess.shtml

Categories