Implementing URL rewriting with wamp and php - 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.

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.

Php laravel route.php does not work on a specific call

My site's directory structure is;
site -> app -> public
--> soundfiles (directory having sounds files)
route.php: Route::get("soundfiles", "controller#soundfiles");
when I hit mysite/soundfiles it shows me soudfiles directory instead of going to => controller#soundfiles.
I want it does not show sound files. where is the problem.
You should try:
Route::get("/soundfiles", "soundfilescontroller#soundfiles");
Route::get('/urlalias', 'controllerName#functionName');
OR
Add this to your htaccess file
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^(.*)$ public/$1 [L]
</IfModule>
Your web server is serving the directory instead of routing everything through index.php. Work out whether you're using Nginx or Apache and ensure you've got it configured correctly. Laravel ships with a valid .htaccess file for Apache, read the docs for how to configure Nginx.

How do I make a subfolder act like a document root using htaccess?

Site location: http://localhost/~username/website
The website loads lots of images with an absolute url such as /image/image.png.
I need that request to go to:
http://localhost/~username/website/image/image.png
instead of
http://localhost/image/image.png.
I also need this to not affect any other folders or the root folder. So that I could also access http://localhost/image/image.png if I wanted to.
Is there some way of making it so that when it's requested from this subfolder to redirect?
I want the absolute reference like /css/something.css and /image/image.png to points to /subdirectory/css/something.css and /subdirectory/image/image.png. That way I don't have to rewrite all the absolute references. So, I don't want to modify the root directory.
I'm wondering if setting up a virtual host that would not allow the subdirectory "website" to have no ability to access the root. I don't ever need root access from this folder.
Be sure you are allowed to use .htaccess file in your webserver. Look at this how to enable in apache:
https://www.digitalocean.com/community/tutorials/how-to-use-the-htaccess-file
Create a .htaccess file in root or ~username/website directory Then write this to your .htaccess file:
Options +FollowSymLinks
RewriteEngine On
RewriteRule ^~username/web/image/(.*) /image/$1
Why the requirement to use /image/image.png? Why wouldn't you just use one of these instead:
http://localhost/~username/website/image/image.png (absolute)
image/image.png (relative without preceding slash)
You can use this rule in root .htaccess OR in Apache/vhost config:
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/~
RewriteRule ^(.*)$ ~username/website/$1 [L]

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

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

Categories