htacces rewrite single file - .htaccess

I want to rewrite a specific file on my website to another one by using htaccess:
# General
RewriteEngine On
RewriteBase /
Options All -Indexes
Options +FollowSymLinks
# Rewrite file
RewriteRule ^/file.html$ /dir/file.html [L]
This is the .htaccess code i'm using based on snippets i found on the internet.
Somehow this is not working, the server is returning a 404-Not-found error.
I can't see any difference with example's that are said to work, like in
Rewriting path for a specific file using htaccess
Edit:
If I place file.html in the root-folder, I can view it. So the rewrite definitely is not happening.

RewriteRule does not receive leading slash. Write so:
RewriteRule ^file.html$ /dir/file.html [L]

Related

rewrite url /blog/article.php?id=hello to /blog/hello.html

Im having some troubles with the redirection rules on my .htaccess file.
I would like to create a rule to redirect my blog content to a friendly url.
The current url structure is:
/blog/article.php?id=hello
and I would like it to change to:
/blog/hello.html
This are my rules so far, and I dont seem to be able to find the error:
RewriteEngine On
Options -MultiViews
RewriteRule ^blog/([a-z,A-Z,0-9]+)$.html blog/article.php?id=$1 [L]
Any help would be appreciated.
Because of your placement of $ in the pattern, the rewrite module is unable to match your request to the expression.
It should be:
Options -MultiViews
RewriteEngine On
RewriteRule ^blog/([\da-z]+)\.html$ blog/article.php?id=$1 [L,NC]

htaccess rewrite query string nothing works

THE PROBLEM
After looking at 50+ StackOverflow posts and trying many permutations of my htaccess file, it does nothing still.
WHAT I HAVE TRIED
Using this website to generate my htaccess file: http://www.generateit.net/mod-rewrite/
Setting AllowOverride All in my httpd.conf file and restarting Apache.
MY CURRENT HTACCESS FILE
Lives in the root directory.
RewriteEngine On
RewriteBase /
RewriteRule ^find-a-local-doctor/([^/]*)/([^/]*)$ /find-a-local-doctor/?state=$1&city=$2 [L]
WHAT I WANT TO ACCOMPLISH
Change this URL:
http://www.md1network.com/find-a-local-doctor/?state=FL&city=Tampa
To this:
http://www.md1network.com/find-a-local-doctor/FL/Tampa
ADDITIONALLY
Since the actual file doing the work is: http://www.md1network.com/find-a-local-doctor/index.php, I need to be able to parse the query string with PHP. Hopefully, I will still be able to do this to get the state and city.
Please help.
Thanks.
Your existing rule looks alright but you will need an additional external redirection rule for reverse. Put this rule before your existing rule (just below RewriteBase /).
# external redirect from actual URL to pretty one
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+(find-a-local-doctor)/(?:index\.php)?\?state=([^&]+)&city=([^&\s]+) [NC]
RewriteRule ^ /%1/%2/%3? [R=301,L]

Remove file extensions using htaccess in subdirectories

I'm trying to remove file extensions with htaccess, so www.mysite.com/file.php becomes www.mysite.com/file.
I'm using the following code in the .htaccess file:
Options +FollowSymLinks
Options +Indexes
RewriteEngine on
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^([^\.]+)$ $1.php [NC,L]
So far, so good. Where it falls down is in subfolders: www.mysite.com/subfolder/file.php
becomes www.mysite.com/file.
I've tried uploading another .htaccess file in the subfolder, but it still does the same. It feels like it should be really simple, but I'm struggling...can anyone help me out? Thanks!
Edit Sorry folks, should have said - the file is in a subfolder like so:
www.mysite.com/folder/subfolder/file.php
The .htaccess file is in /folder, the URL changes to this format:
www.mysite.com/subfolder/file
Apologies for misleading.
This is the rule you'll need to hide .php extension. This goes into your .htaccess in the DOCUMENT_ROOT directory:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
# To internally redirect /dir/foo to /dir/foo.php
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^ %{REQUEST_URI}.php [L]
You shouldn't be using rewrite rules for this. Apache has built-in option explicitly for doing what you're trying to do, called MultiViews, and that's what you should be using:
The effect of MultiViews is as follows: if the server receives a request for /some/dir/foo, if /some/dir has MultiViews enabled, and /some/dir/foo does not exist, then the server reads the directory looking for files named foo.*, and effectively fakes up a type map which names all those files, assigning them the same media types and content-encodings it would have if the client had asked for one of them by name. It then chooses the best match to the client's requirements.
Just add Options MultiViews to your .htaccess, and make sure that AllowOverride is properly configured.
A guess in the wild
Try
RewriteRule ^(.+)$ $1.php [NC,L]

rewrite php file in htaccess with our without query string

So I have photographs.php, and there are also sections where it will be rewritten as photographs.php?cat=somecategory
Question is, in .htaccess how do I get these both to work as such
/photographs
and
/photographs/somecategory
so that the $_GET['cat'] variable will be = somecategory
/photographs/(.+) will need to redirect to photographs.php?cat=$1, while /photographs/? will just redirect to photographs.php. if you want to be clever, you can combine it and do it in one line and /photograph will just go to photographs.php?cat=[blank].
First of all you must have the mod_rewrite module activated.
Here's what you need to add to your .htaccess file:
Options +FollowSymlinks
RewriteEngine on
RewriteRule ^photographs$ photographs.php [NC]
RewriteRule ^photographs/(.*)$ photographs.php?cat=$1 [NC]
Just some simple replace rules. You can find lots of info on the web about them.

URL rewrite using mod_rewrite in .htaccess

I am trying to rewrite the URL using the mod_rewrite apache module.
I am trying to use it as below :
Options +FollowSymlinks
RewriteEngine on
RewriteBase /
RewriteRule ^wants/testing.htm wants.php?wantid=$1 [L]
I have created a .htaccess file in the directory from where the files are accessed.
The mod_rewrite is also enabled.
With all these done, i still haven't been able to get it working.
Can someone please help me with this? Please let me know if i am missing something
Thanks in Advance,
Gnanesh
As per OP's comment:
The URL shows
mydomain.com/wants.php?wantid=123. I
need to make it look like
mydomain.com/wants/123
This should work for your case:
RewriteEngine on
RewriteBase /
RewriteRule ^wants/([0-9]+)$ /wants.php?wantid=$1 [L]
It will allow you to use http://yoursite.com/wants/123 and will silently rewrite it to wants.php?wantid=123
I think a leading slash (or rather the lack thereof) might be yoru problem:
Options +FollowSymlinks
RewriteEngine on
RewriteBase /
RewriteRule ^wants/testing.htm /wants.php?wantid=$1 [L]
Otherwise Apache might be looking for the PHP file in /wants/wants.php as it'll be treating it as a relative URL.
Hmm, so I gave it some thought and I guess you could do something like this ( only changed the regexp according to your comment, if I understood correctly ):
Options +FollowSymlinks
RewriteEngine on
RewriteBase /
RewriteRule ^wants/\d+$ /wants.php?wantid=$1 [L]
But I guess you could also leave out the $1 reference, and still be able to access the id in your wants.php. So
RewriteRule ^wants/\d+$ /wants.php [L]
should work too, and you can then use something like
<?php
$request = split('/', $_SERVER["REQUEST_URI"])[1];
?>
in your wants.php where the last element of the array would be your id ( or anything else you ever decide to rewrite and send to the script ).
If you want to use the rule in the .htaccess file in your wants directory, you have to strip the contextual wants/ from the start of the pattern. So just:
RewriteEngine on
RewriteRule ^([0-9]+)$ /wants.php?wantid=$1 [L]
Otherwise, if you want to use the rule in the .htaccess file in your root directory:
RewriteEngine on
RewriteRule ^wants/([0-9]+)$ wants.php?wantid=$1 [L]

Resources