how to keep htaccess file under site root - php

localhost, xampp, win 7
site path:
C:\xampp\htdocs\news\s02
so all relevant files, including .htaccess is under s02 folder.
links like this work fine:
http://localhost/news/s02/view.php?art=160915142500&title=blue-sky
I want the following computed url
http://localhost/articles/160915142500/blue-sky
.htaccess
RewriteEngine on
RewriteBase /
RewriteRule ^articles/([0-9]+)/([a-zA-Z-_]*)$ view.php?art=$1&title=$2 [NC,L]
result - error 404
httpd.conf
LoadModule rewrite_module modules/mod_rewrite.so - uncommented
AllowOverride All - all instances
When I move .htaccess file to:
C:\xampp\htdocs
and change RewriteRule to:
RewriteRule ^articles/([0-9]+)/([a-zA-Z-_]*)$ news/s02/view.php?art=$1&title=$2 [NC,L]
there is no error 404, i.e. view.php is loaded, but without images, css and js files (they are all under previous, i.e. s02 folder.
How to keep .htaccess file under site root (s02) and load view.php with all its elements?
restarting xampp and apache and mysql modules doesn't help.

I don't think this is possible to do from a sub directory. You are basically trying to rewrite a request sent to the root directory from within a sub directory. In order to rewrite the current url, apache should see the .htaccess file in the current directory.
However you can still redirect to the specified location, but you have to make the .htaccess file in the htdocs directory.
This is my first answer :) Not sure if this is suitable as an answer.

Related

Pathing issues when I try to set my .htaccess file to my main file location. (cpanel)

I'm trying to set up the base path/file for my site on my server. The file I want to load is in my document root within the following path:
/src/pages/views/index.php
I'm trying to get my .htaccess file to point to this location by default. I have tried doing this
DirectoryIndex /src/pages/views/index.php
which does automatically route to my index file, but then all relative paths in my site no longer work. (My url bar shows just 'mywebsite.com')
If I don't have anything in my .htaccess file, then I go to mywebsite.com (which shows me the root directories) and manually click my way into the directory that I want, everything works just fine and there are no pathing issues (my url bar now shows 'mywebsite.com/src/pages/views/').
By "pathing issues", I mean all of my imported files (javascript, css, etc) are 404ing because it can't find the path.
Is there a way to actually make it so that when a user types in :
mywebsite.com
it will actually redirect them to the proper path? :
mywebsite.com/src/pages/views/
Give this a try:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^mywebsite.com$ [NC]
RewriteRule ^(.*)$ http://www.mywebsite.com/src/pages/views/$1 [R=301,L]
You can also try:
RewriteEngine on
RewriteRule ^mywebsite\.com$ /mywebsite.com/src/pages/views/?&%{QUERY_STRING}

Set .htaccess at localhost and hide index.php

I wanna ask about .htaccess that could be change the url to be more simple. I have the url in my localhost like this:
localhost/spsb/index.php
and
localhost/spsb/index.php?page=student
The question is:
How can i change the url above being:
localhost/spsb
and
localhost/spsb/student
And where is the correct place to save the .htaccess file?
Thank's a lot for advice
I have been searching a lot everywhere but no one can solve my problem
Use URL Rewriting
For an Apache server:
First, you must activate the rewrite module
(How to active mod_rewrite)
Add those lines to your htaccess to enable rewrite engine
Options +FollowSymlinks
RewriteEngine On
And this line to rewrite the url as you want
RewriteRule ^spsb\/?$ spsb/index.php
RewriteRule ^spsb/([a-zA-Z]+)$ spsb/index.php?page=$1 [L]
For a Nginx server:
Put these lines in "Location" scope:
if (!-e $request_filename)
{
rewrite ^spsb\/(.*)$ spsb/index.php?page=$1;
}
Note: the location of .htaccess is relative to his level into the site structure, if he is inside the first level site/dir/ the contained rules are limited to site/dir structure
To be unlimited, i suggest to put it into the document root
...Or you can put the rules directly into you vhost configuration
How to check errors:
First have you restarted your server to apply the activated rewrite
modules
(rules from htaccess dont require a restart, only module
modification)
Add following lines to your htaccess after "RewriteEngine On":
RewriteLog "/var/log/apache2/{your_log_site...}" <-- put your path
RewriteLogLevel 7
Check by a "tail -f /var/log/apache2/you_log_site.errorlog" to see if
an error appear at resfresh page

change security permission in a website

I have a website that is working properly.I dont know when I do "Domain-name.com/images" It shows me all the images in the images folder present at my site.I dont know why is this.may be this is due to the Directory permissions?But I want to ask know the actual reason behind it
Help will be appreciated.
Note:I am tagging Php and Html because these people might faced this thing while creating website.
This is because there is no index file in the folder, and Apache (assuming Apache) is set to do directory indexes.
Either create an empty index.html or add the following in either apache2.conf (or httpd.conf) or in a htaccess file:
Options -Indexes
You can restrict the folders using .htaccess.
Create .htaccess file in you website root folder and add the following code in it.
RewriteEngine on
RewriteCond $1 !^(css|js|images)
RewriteRule ^(.*)$ index.php/$1 [L]
This is a problem with the configuration of your web server which allows directory listing for your image folder. E.g. on Apache, the most common server software, you would switch it off in the httpd.conf with the directive Options -Indexes in a directory section.
To answer your question: yes. If it's a web accessible directory meaning it resides in the typical webroot folder such as public_html, www, etc and the permissions on the folder are open then anyone can see the contents.

.htaccess - mod_rewrite is working for some pages but not for others

I am stuck into a weird problem.
I have a file at the location /public_html/academics/courses.php
I want .htaccess to mod_rewrite the URLs as below:
Original URL: http://niecdelhi.ac.in/academics/courses/
After mod_rewrite: http://niecdelhi.ac.in/index.php?inc=/academics/courses/
What I want, basically, is to mod_rewrite all URLs to index.php and pass the URL as a parameter named "inc". Then, in the index.php I include the file by doing include($_GET['inc']);
mod_rewrite is working for some pages on the website. and I am getting the URL in $inc. But, it is not working at all for other pages.
For example, consider the two files that exist on the server:
http://niecdelhi.ac.in/academics/courses.php
http://niecdelhi.ac.in/academics/library.php
mod_rewrite is working for the first, the file gets included in index.php
But for the second I get the plain existing file. not the one included in index.php
I hope you understand the problem that I am facing. Please provide me with the solution.
.htaccess file
Options +FollowSymlinks
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} !\..*$ [NC]
RewriteCond %{QUERY_STRING} (.*) [NC]
RewriteRule ^(.*)$ index.php?inc=$1&%1 [L]
Page working: http://niecdelhi.ac.in/academics/courses/
Page not working: http://niecdelhi.ac.in/academics/library/
*EDIT*
There is no other .htaccess anywhere. Although, I have found a clue about what is happening. The problem is happening only in Linux server. The code is working correctly in Windows server.I have a Linux server with PHP 5.2.16.
Also, regarding some pages working and some not. I have found that only those pages are working which have a folder with identical name in the same directory. For example, The academics directory is as below:
academics/
|_ courses/
| |_ mba.php
| |_ mca.php
|_ courses.php
|_ library.php
Now, Since courses.php has a folder with identical name in same directory. It gets mod_rewrite fine. But library.php is not getting mod_rewrite.
Linux server is skipping the mod_rewrite for the files that actually exist. Why so ??
My only guess is that there is another .htaccess somewhere in your structure (most obviously inside /academics/ ) that is overriding the rule for "library".
Could there be another mod_rewriting rule inside that folder that is kicking in for the word LIBRARY and probably messing up your rewriting.
Note that it might also be a native apache issue. For example, in ubuntu, by default in version 10.10 (i think thats it) if you had a /javascript/ folder, it would be short circuited to /usr/lib/javascript or something like that...
Check all possible instances of mod rewrite in httpd.conf, all dynamicaly loaded .conf files, your vhost file and finaly the path of your document root...

Local Apache Server mod_rewrite/pretty links issue

I've had to move from testing on the live server, to testing locally on a virtual apache server. I've installed XAMPP just fine, downloaded and installed the wordpress files and the database. Everything looks great! The local version of my homepage is identical to the live version. There's only one problem: the homepage is the only page that works. When I click on one of the links i.e. the "about" page (http://localhost/wordpress/about/), I am redirected to the xampp control panel (http://localhost/xampp).
I have a good feeling this has to do with a problem with the "pretty links"/mod_rewrite rules. I made sure I brought over the .htaccess file, and it contains the rewrite instructions. The wordpress database has the proper permalink structure, and the httpd.conf file has the "RewriteEngine on" and the "FollowSymLinks" directives enabled. There has got to be some sort of rewrite problem here, although I am not ruling out something else stupid I might have done. Thanks for all your help!
-E
*Here is what the .htaccess looks like:
# BEGIN WordPress
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
# END WordPress
In the httpd.conf, change the
DocumentRoot "/path/to/your/app/wordpress"
also
# This should be changed to whatever you set DocumentRoot to.
#
<Directory "/path/to/your/app/wordpress">
This should work, the path is absolute.
And do you have load the:
LoadModule rewrite_module modules/mod_rewrite.so
in httpd.conf??
If your server version works with a domain name, i.e: http://domainname.com is equivalent that http://localhost/wordpress the rewrite rules will be differents. post the rules here.
The problem is that wordpress does not believe in relative paths for some reason. There is an assumption that wordpress is running from the server root not a directory under the root (e.g. /var/www/wordpress will not work, but /var/www/ will).
The problem is with the .htaccess file they provide. It should re-write it to index.php and not /index.php. Change that line in your config and it will work.
What happens is that it tries to actually go to the default document root (in my case /var/www/index.php, which does not exist since I am using http://localhost/worpress which is an alias for ~/projects/worpress). You can check your error log and it will tell you where it is trying to look for the index.php file (which will return a 404 error).
I can go on a rant about how stupid it is that they do that and how bad the whole software design of wordpress is. But I will spare you that :).

Categories