PHP / .htaccess - Redirect to specific folder - php

I am creating a PHP project, and the folder structure looks something like:
includes/
pages/
config/
In theory, all of the pages will go into the pages folder. But, whenever someone visits the website, on a specific page: (i.e.) www.mysite.com/help I want it to look inside the pages/ folder, rather than thinking it is on the root of the document.
Can I achieve this using PHP / .htaccess - I have googled this problem and cannot see any relevant infomation

You can use the following rule in /root/.htaccess :
RewriteEngine on
#if "/document_root/pages/foo" is an existent file
RewriteCond %{DOCUMENT_ROOT}/pages/$1 -f
#rewrite /foo to /pages/foo
RewriteRule ^(.*)$ /pages/$1 [NC,L]

Apache supports URL rewriting as explained here:
http://httpd.apache.org/docs/2.0/misc/rewriteguide.html

.htaccess
RewriteEngine on
RewriteRule ^/?help/?(.*) /pages/$1 [L]

Related

htaccess RewriteRule using slash as get params: can't access other files in the folder

I have a simple user system in my site. The URL works like this: /profile/user?id=user_id
I wanted to do something like /profile/user/user_id instead, and I found the following code to use in htaccess.
RewriteRule ^user/(.*)$ user.php?id=$1 [L]
The code does work as intended, but after doing that I found out that I can't access any other file inside of the profile directory besides the index file. All of them return a 404 error.
There's a couple more of important pages in there, so I would need to access them. I can't access user.php without an ID either, but that's not my main concern, even though I would also like to access it if possible.
I'm not really used to use RewriteRule, so I'm not really sure what's going on here. What can I do?
EDIT: I was asked to give more info, here we go.
The profile folder full route is /en/profile
Structure of the profile folder:
profile/settings.php (directory index)
profile/user.php
profile/change_avatar.php
profile/change_avatar_upload.php
profile/change_email.php
profile/change_password.php
profile/custom_utgen_characters.php
profile/custom_utgen_character_editor.php
profile/reset_password.php
Other lines in the htaccess file:
DirectoryIndex settings.php
htaccess file in the root folder of the website:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R,L]
This is all the relevant option I could get, I hope it helps.
I tested your rewrite rule and directory structure and had no problems to access files in the profile directory. Can you provide more information like the complete .htaccess file and the directory structure laying under profile?
EDIT: The .htaccess in your profile folder overwrites the .htaccess in your root folder. To prevent this, you have to add the line
RewriteOptions inherit
to the .htaccess file in the profile folder.

Url RewriteRule with php not able get the path [duplicate]

I have a single point of entry on my website
mysite.com/admin/index.php?View=List&Model=User
All access to the DB/application is through this index.php file.
I would like to clean my URL from what is shown above to.
mysite.com/admin/List/User
The idea is to remove the key 'Model and 'View'.
I have found this link that looks very similar.
PHP/Apache: Rewrite rules with .htaccess
in htaccess file insert this code :
RewriteEngine On
RewriteRule ^admin/([^/]*)/([^/]*)$ /admin/index.php?View=$1&Model=$2 [L]
The rewritten URL:
http://mysite.com/admin/List/User
this site very useful for generate rewrite URL
http://www.generateit.net/mod-rewrite/
Create a file named .htaccess in your web root if you haven't already got one with the following:
# Turn the rewrite engine on (skip this if already done)
RewriteEngine On
# Redirect admin/List/User/ to admin/index.php?View=List&Model=User
RewriteRule ^admin/([^/.]+)/([^/.]+)/?$ admin/index.php?View=$1&Model=$2 [L]
This might work:
Options +FollowSymlinks
RewriteEngine on
RewriteRule ^admin/([^/]+)/([^/]+) /index.php?View=$1&Model=$2 [NC]
Add to .htaccess in the root folder of your website.

htaccess redirect image folder to root

I have a directory called "public_html/hammer/" (my CMS). I want to use the image folder that is on the root "public_html/". I could simply do something like "../picture.jpg" but the content within "/hammer/" will be show on the root directory so it will then be broken when I query the html.
How can I write an ModRewrite or similar that will point "public_html/hammer/images/" to "public_html/images"?
Assuming public_html is your document root, place a rule like the following in public_html/.htaccess:
RewriteEngine On
RewriteRule ^hammer/images/(.*)$ images/$1 [L]
Basically:
RewriteEngine on
RewriteRule ^.*$ ../../$1 [L]

.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]

How to use rewriterule to redirect to a php file in the same folder?

I would like to take requests for /somefolder/style.css and handle them with /somefolder/program.php
So, I put the following in my .htaccess file:
rewriteengine on
rewriterule ^style.css$ program.php?css=1 [R=302,L]
The result is that instead of redirecting to /somefolder/program.php, the server tries to redirect to:
/var/www/html/somefolder/program.php?css=1
How can I get rid of the /var/www/html/ in the redirect? I thought that since I just entered program.php in the .htaccess that it would default to the same folder.
Since this is a generic script that I will use in many places, I would like to avoid using rewritebase to specify the folder I'm in -- the .htaccess has to work in any folder without being modified.
Leave the R flag away and you will get an internal redirect:
RewriteRule ^style\.css$ program.php?css=1 [L]
Otherwise specify the full URL path you want to redirect to externally:
RewriteRule ^style\.css$ /program.php?css=1 [R=302,L]
Or for any arbitrary folder:
RewriteCond %{REQUEST_URI} (.*)/style\.css$
RewriteRule ^style\.css$ %1/program.php?css=1 [R=302,L]
I think the problem is that you are missing a ReWrite base statement.
Also the I would put the .htaccess file in the root directory of your site. That way you don't have to copy an .htacess file into every new directory you create. What if you want to change the name of your php file? You'd have to change every .htaccess file.
This is how I would redirect www.mydomain.com/orange/style.css to www.mydomain.com/orange/program.php?css=1 using generic rules:
RewriteEngine On
RewriteBase /
RewriteRule ^(.*)/style\.css$ $1/program.php?css=1 [L]

Categories