Set .htaccess at localhost and hide index.php - 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

Related

how to keep htaccess file under site root

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.

Implementing URL rewriting with wamp and php

I can't implement URL rewriting.
What I've done so far:
1. Enabled rewrite_module in apache.
2. Added a .htaccess file to my website (test) in the directory:
127.0.0.1/test/
which contains the following:
RewriteEngine on
RewriteRule ^/news/([0-9]+)\.html /news.php?news_id=$1
3. I have a news.php file which contains
echo "hello world";
Now, whenever I go to URL: http://127.0.0.1/news/1, I get a
'Page not found'
I was successful in the following.
Files:
document_root/
|- .htaccess
|- news.php
.htaccess:
RewriteEngine on
RewriteRule news/([0-9]+)\.html news\.php?news_id=$1
http://127.0.0.1/news/1.html
-> hello world
I suppose that you've successfully enabled the rewrite module (rewrite_module) of your Apache server, and that you've put your .htaccess file inside the test directory which is in the root directory of your web server ( like for example : c:\wamp\www\test).
So here we have 2 cases of what (I suppose) you want to do :
Access to your URL using, for example : http://127.0.0.1/test/news/1.html.
Access to your URL using, for example : http://127.0.0.1/news/1.html.
So, for the 1st case, you have just to edit your .htaccess file like this :
RewriteEngine on
RewriteRule ^news/([0-9]+)\.html news.php?news_id=$1
Here any URL like http://127.0.0.1/test/news/1.html will be rewriting to test\news.php and pass to it the news_id param.
And for the 2nd case, you have to put your .htaccess file into the web server root directory and add just the RewriteBase directive
The RewriteBase directive specifies the URL prefix to be used for per-directory (htaccess) RewriteRule directives that substitute a relative path.
so your .htaccess will be like this :
RewriteEngine on
RewriteBase /test
RewriteRule ^news/([0-9]+)\.html news.php?news_id=$1
Of course you can find many many tutorials on the net about how to rewrite toyr URLs using the .htaccess file ...
Hope that can help.

.htaccess url rewrite not passing parameter - could there be apache settings somewhere that is preventing this?

Okay, so this problem has completely stumped me and the other devs I work with. Here is the rundown:
I have a local dev environment setup with Mac Apache2 pointed at /Users/myusername/Sites/
Within /Sites I have two folders, /site-1 and /site-2, both of which have virtual hosts pointed at them site-1.dev & site-2.dev. Both site-1 and site-2 are running local installs of PerchCMS.
Within /site-2 I have an .htaccess file which I am trying to set up a URL rewrite that takes the URL /detail/slug-here and translates it into /detail.php?s=slug-here
I have tried the following rewrites (at the suggestion of PerchCMS support) and both have failed to pass the s param:
RewriteRule ^detail/([a-zA-Z0-9-/]+)$ detail.php?s=$1 [L]
RewriteRule ^site-2/detail/([a-zA-Z0-9-/]+)$ /site-2/detail.php?s=$1 [L]
Additional info:
Yes mod_rewrite is enabled in apache... in the same .htaccess file it totally works if I do a simple rewrite like this...
RewriteRule dangerzone.html index.php
One odd behavior that I've noticed is that if I remove everything from .htaccess I can still pull up detail.php by pointing my browser at /detail/test-item-1...(yes I have restarted my server) so its behaving as if there is still some sort of rewrite in place and loading detail.php sans param just as it continues to do with the rewrite in place - is this a clue that there is something off somewhere else in my server config? Note, RewriteRule dangerzone.html index.php does NOT work once it is removed from .htaccess.
Have this code in your site root .htaccess (inside /site-2/):
Options -MultiViews
RewriteEngine On
RewriteRule ^detail/([a-zA-Z0-9/-]+)/?$ detail.php?s=$1 [L,QSA,NC]
Important is to turn off MultiViews options here. Option MultiViews is used by Apache's content negotiation module that runs before mod_rewrite and makes Apache server match extensions of files. So /file can be in URL but it will serve /file.php.

.htaccess redirect to URL when match on certain file

Under my root web directory, I have this two files:
aboutus.php
about-us.php
Now going to this URL http://local.com/about-us.php will render the file about-us.php. If I will do the inverse, how can I dictate the .htaccess that whenever the URL above is access, the aboutus.php will be rendered?
If you have access to the whole server config, the most efficient way to do this is to use mod_alias. Unfortunately this needs to be done in VirtualHost config - which is only accessible if you got root access to that server.
Alias /about-us.php /full/local/path/to/aboutus.php
If you cannot edit the VirtualHost config, use mod_rewrite (needs more server resources though, as every request has to be matched to those rules):
RewriteEngine On
RewriteRule ^about-us.php aboutus.php [L]
Should do the trick.

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