rewrite Urls using htacess - php

I have a website as a side hobby.
I purchase a custom script from a developer. That script is designed to be run on the main domain. like www.example.com
This is because of the script using require_once and include_path function to call files within the script. For example /functions.php or /admin.php
So if I configure it on the main domain it works but on the subdomain, it gives 403 Forbidden error because of the nonexistence of URLs.
I mean for subdomain the directories inside it should be changed to
subdomain.example.com/functions.php or subdomain.example.com/admin.php.
Here is the problem there are more than 100+ pages and it would be quite hard to change it one by one for me.
I am looking if I can do it using .htacess rewrite module.
Is it possible if on page load the directories inside the files can be altered?

Related

How to forbid direct access to website directories in browser url, but allow when website requests them internally?

I have Apache+Nginx Hybrid server setup running a website with php/mysql backend.
And for security reasons, I am trying to restrict access to my website's internal folders/directories like css, Js and other included php files from direct access of users.
For example if user types https://mywebsite.com/css or https://mywebsite.com/included-php-files/ in the browser url
then it should give him 403 forbidden directory access error.
But when those directories have to be accessed via internal files like index.php in it's Ajax request or Internal inclusions then those directories should be allowed to have access.
I have tried many solutions but they are permanently blocking access even to my website internal files.
/css
/Js
/admin
/sitemap/sitemap files/
These are example of my directories inside my website, I need to hide them from direct access of users from abusing their use.
Last thing I have tried is I added an empty index.php file to each of those folders where I want to stop direct external access, this way when users try to access those folder or directories, It will show them empty page.
But I am hoping if there is some other and better way of doing so, like with nginx or apache ?
Please help me..
I will update if need more clarity about the question..
just use .htaccess file
Put the following line into your .htaccess file
Options -Indexes

htaccess rewrite rules to force a 404 up one directory levek while maintaining the request URL

I am working on a website that has many directories and subdirectories. Developing locally, I am running php's built in webserver. I really like the way this handles 404s in that if a 404 is found, it serves the parent directory, but maintains the url so that I may parse it and perform some task.
For example:
Given: dir4 does not exist
localhost:8000/dir1/dir2/dir3/dir4/
This URL will return the index.php found in dir3, however the URL will remain the same, and I can use dir4 to determine some login on the dir3 index file (looking at $_SERVER['REQUEST_URI']).
The behavior is also outlined here.
When I move my project onto my webserver, this did not work.
I've tried the solution posted here but it removed the "dir4" part of the URL.
I am hoping to recreate this functionality via .htaccess. Has anyone successfully done so?

php routing old php site to new ror site

i have a site abc.com in php and now made anothers site abcd.com on rails, i wish to redirect all requests that go to the php site to redirect to abcd.com...
example:
abc.com/page3 -> abcd.com/page3
abc.com/non_existing_link ->abcd.com
this php site is just hard coded, not using any framework and i wish to write script in index.php which would accept ny link towards abc.com and then check the extension and redirect it accordingly to the corresponding links on abcd.com... i was suggested to write a controller from a collegue and i am not familiar with mvc functionalities nor th controllers class and stuff...
If your webserver runs apache, I would recommend placing a .htaccess-file in the root of your original server as in the following answer: .htaccess redirect all pages to new domain. If you would like to keep the names the same (redirect to exactly the same name on your new domain), use the following lines:
RewriteEngine On
RewriteRule ^(.*)$ http://newdomain.com/$1 [R=301,QSA]
This method works with very limited amount of work/coding :-)
I believe you there are several ways to achieve so:
Using php redirect in all php pages.
How to make a redirect in PHP?
Modifying .htaccess file in your directory
http://kb.mediatemple.net/questions/242/How+do+I+redirect+my+site+using+a+.htaccess+file%3F
If you have a cpanel of other controler system from hosting provider then do it directly there.

Server File Configuration: Using .htaccess to redirect to a script but pass CGI arguments aswell

I have an idea that will allow a Web Forums Content/Threads to be better indexed by search engines but avoid taking up too much unnecessary space on the web server.
My idea is not unique(I think StackOverflow uses it) but I am having difficulty working out how I am going to achieve redirecting through .htaccess commands or main server configuration files.
For a web forum website; when a new thread is created:
I store the thread HTML in an SQL database(rather than creating a HTML file which I think will take up more server space - is that correct?).
I create a directory on the server where the directory's name is the threads name(this will allow for easier indexing from google & other search engines wont it? Because the url is more descriptive?). So www.myForum.com/posts/unique_thread_name/. I think this is how StackOverflow does this, if you look at the url of my question its a directory.
The new directory(thread directory) will be empty except for a .htaccess file which will redirect to a script www.myForum.com/cgi-bin/loadWebpage.py. This script will grab the thread's HTML from the database when accessed & display that thread.
So when someone accesses www.myForum.com/posts/unique_thread_name/, the .htaccess file will redirect to www.myForum.com/cgi-bin/loadWebpage.py?thread=unique_thread_name. Notice the arguments, is it possible for a .htaccess file to redirect to a script but pass arguments aswell?
My Questions:
The apache website says you should never use .htaccess files "In general, you should never use .htaccess files unless you don't have access to the main server configuration file.". If I am using a webhost like GoDaddy do I have access to this file or is this server config file only for VPS?
Is it better to do this with the Server Config file instead?
Is there a way easier way of doing this? The whole idea is to store Forum Threads that take up as little space as possible but are still easy for search engines to index(thus the unique directories created).
Do I NOT need to create a .htaccess file in each post directory? Can I just write in my main .htaccess file that any request to a file/folder in posts should redirect to www.myForum.com/cgi-bin/loadWebpage.py?thread=the directory they accessed?
Maybe the code would look something like this?
Redirect /posts/* www.myForum.com/cgi-bin/loadWebpage.py?thread="HOW DO I SPECIFY THE FOLDER?"
If I am using a webhost like GoDaddy do I have access to this file or is this server config file only for VPS?
Godaddy shared hosting only allows .htaccess use.
Is it better to do this with the Server Config file instead?
Its better performing if you have access to Server Config, but shared hosting like Godaddy does not allow it.
Do I NOT need to create a .htaccess file in each post directory? Can I just write in my main .htaccess file that any request to a file/folder in posts should redirect to www.myForum.com/cgi-bin/loadWebpage.py?thread=the directory they accessed?
You can do it with a single .htaccess in the root dir of your site with contents as below
RewriteEngine on
RewriteBase /
RewriteRule ^posts/(.+)/$ /cgi-bin/loadWebpage.py?thread=$1 [NC,L]

Why would index.php only work some of the time?

I just moved a develoment site onto a test production server and I'm testing some things out.
When you go to the root URL (ie rooturl.com), the browser is correctly rendering rooturl.com/index.php without showing the index.php in the address bar.
However, I also have a directory at rooturl.com/admin that also has an index file of index.php, but when I go to rooturl.com/admin I'm getting a 404 not found error. But if I type out rooturl.com/admin/index.php, it loads the page.
Is there a common reason for this?
The last piece of relevant information is that since my client won't switch their domain name to the new host until they are ready with migrating email, etc., I'm currently not able to view the site on the registered URL, I can only access it using the IP address directly which I got from the host.
My gut feeling is that the direct IP address is screwing with how it would normally work, but it's just a guess and I have no idea why that would be the case.
Sounds like it could be an .htaccess rewriting issue. Be sure to check any .htaccess file (hidden, by default) in those directories for any screwy rewrite-rules.
If you're running a MVC project, then the url /admin may be confused for a call to a controller rather than a directory.
My first shoot is that you have .htaccess file and it is redirecting everythind to your root index.php file.

Categories