Rewrite rule in Apache/htaccess doesn't work - apache

This is what is in my htaccess file which is located in the same directory as my php project:
Options +FollowSymLinks
Options +Indexes
RewriteEngine On
RewriteBase /
I have got one link on my page that sends the user to this address:
http://localhost/PHPTest/index.php?article=27
This is the output that I want== index.php/article_27 ?
UPDATE:
When I press the link, this is the output in the url:
http://localhost/PHPTest/index.php?article=27
This should be intercepted in the .htaccess in my php project folder and swap it to this:
http://localhost/PHPTest/index.php/article_id_27
Here is my rewrite rule. But it doesnt work:
RewriteRule ^index.php?article=([0-9]+)$ index.php/article_id_$1
More information. There is my project path, where index php is:
C:\xampp\htdocs\PHPTest

The url doesnt display itself as index.php/article_27
Why would it? You're pointing the link to ...article=27 and your rule rewrites from ...article_27 to ...article=27.
In the scenario you have given, you probably want a redirect from ...article=27 to ...article_27, not a rewrite.

Related

RewriteRule in htaccess returns the error Not found

I'm trying the second way to run PHP code in HTMl file using a rule in .htaccess file as described by the link https://stackoverflow.com/a/6237056/3208225
RewriteEngine on
RewriteRule ^(.*)\.html $1\.php
But when I try opening my page e.g. test.html I receive
Not Found
The requested URL /test.html was not found on this server.
Why and how to resolve?
UPDATED (from comments)
I try both on localhost and on my shared hosting. htaccess and html files are in document root. BTW, the homepage (index.html) also returns Not found. In local machine the path is D:/Server/vhosts/another. And without this htaccess such virtual host works just fine. So there is no issue with its configuration.
With your shown samples please try following .htaccess rules file. Make sure your htaccess is present along with your php files only. Also clear your browser cache before testing your URLs.
RewriteEngine ON
RewriteRule ^(.*)\.html/?$ $1.php [NC,L]
Fix added by OP: Please note such rule is not execution of PHP code inside of HTML file, but just a redirecting from HTML file to PHP file, so the second file also must exist

Access external files as local files with htaccess

I have a source and several user domains. I'm keeping JS, CSS and image files on the source domain and in the user domains I'm including those stuffs like source.com/js/jquery.js but I want to show them like user1.com/js/jquery.js, user2.com/js/jquery.js without the files physically there.
source.com/css/bootstrap.css
source.com/js/jquery.js
source.com/user/1/library/picture.png
source.com/user/2/library/picture.png
user1.com/css/bootstrap.css
user1.com/js/jquery.js
user1.com/library/picture.png
user2.com/css/bootstrap.css
user2.com/js/jquery.js
user2.com/library/picture.png
Is this possible with htaccess? And for helping, all of these source.com, user1.com, user2.com are in the same server.
Thanks for any help.
Okay, test this :
in user1.com .htaccess use RewriteRule
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^js/jquery.js$ http://source.com/js/jquery.js [P]
</IfModule>
then your url is something like this http://user1.com/js/jquery.js and file will be read from http://source.com/js/jquery.js without changing url.
This worked for me.

How to redirect path to a different folder using mod_rewrite/mod_alias?

I have mod_rewrite and mod_alias enabled. I have some csv files saved in /home/ubuntu/csv. Due to reasons, I can't move files to any other folder.
My website url redirects to /var/www/html. So wesbite-url.com would redirect to /var/www/html.
What I want is somehow website-url.com/files/a.csv should redirect to /home/ubuntu/csv/a.csv? Basically I want to access any file, say x, saved in /home/ubuntu/csv via website-url.com/files/x
How do I go about doing this?
I tried the following commands among others:
RedirectMatch ^/files/$ /home/ubuntu/portalCsv/
RedirectMatch ^/files/ /home/ubuntu/portalCsv/
Redirect /files/a /home/ubuntu/portalCsv/
What is the correct command for this?
You need to capture the url part after /files/ and append it to the Redirect destination
RedirectMatch ^/files/(.+)$ /home/ubuntu/portalCsv/$1
This will redirect your browser from /files/foobar to /home/ubuntu/portalCsv/foobar .
if you want to redirect using mod-rewrite,you can use the following :
RewriteEngine on
RewriteRule ^/?files/(.+)$ /home/ubuntu/portalCsv/$1 [L,R]

How Remove a folder form url using .htacces

I develop a new site for a client and I don't want to move to the root folder of the server.
Besides that, the hosting service do not alow me to change the physical path.
So I decide to use .htaccess to handle this.
My .htaccess file is like this:
AddHandler php56-script .php
suPHP_ConfigPath /home/balletpaulacastro/
Options +FollowSymLinks
RewriteEngine on
So when the user try to access www.balletpaulacastro.com.br they will be redirected to www.balletpaulacastro.com.br/bpc where the new site is located.
What i want is to rewrite the url to not show de /bpc/ folder.
I try many examples, but i'm not a coder, so if you guys can write down the entire code with my actual url and folders, that will be so nice.
Thanks in advance
Raul
Try :
RewriteEngine On
RewriteRule ^((?!bpc).*)$ /bpc/$1 [NC,L,QSA]
This will rewrite
example.com
to
example.com/bpc

htaccess rewrite directory/file references to new url

I'm trying to rewrite all files located below a URL such as:
http://www.example.com/one/
to a new URL:
http://www.newhome.com/new/
So, the desired functionality is to have http://www.example.com/asdf and http://www.example.com/ both remain on www.example.com, but http://www.example.com/one/test/index.php would pull content from:
http://www.newhome.com/new/test/index.php
I can't quite get the correct rewrite rule to do this. Any ideas?
EDIT: The rewrite rule needs to have requests from http://www.example.com/one/* retain the URL in the browser, but pull content from http://www.newhome.com/new/test/* (the rewritten locations will include forms and form submissions).
Of note, the .htaccess file will go in the equivalent of the '/one' directory (because of server access restrictions)
Thank you
Put this rule as your first rule in your DOCUMENT_ROOT/.htaccess file:
RewriteEngine On
RewriteRule ^one/.*$ /new/test/index.php [L,NC]