What I'm ultimately looking to do is do /about instead of /main.php?p=about. I've seen a few posts (like this!) but none seem to work from me.
Firstly, I was putting the ReWrite rules in httpd.conf..is this correct?
Lots of places mention .htaccess but I didn't have that file in any directory that seems important (the only references are in phpMyAdmin). I copied the file from phpMyAdmin into htdocs and then replaced the text with a simple rule:
RewriteEngine on
RewriteRule home main.php [L]
However localhost/home reported a Bad Request.
So firstly which file should I use & if .htaccess where should I put the file & do I have to reference it in any other files? Secondly, is the following likely to work for my desired approach?
RewriteEngine on
RewriteRule ^([^/]+)/? main.php?p=$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
Thanks for any clarity & help.
Mike
You actually have conditions (RewriteCond) after RewriteRule. It should be other way round.
Enable mod_rewrite and .htaccess through httpd.conf and then put this code in your DOCUMENT_ROOT/.htaccess file:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+)$ /main.php?p=$1 [L,QSA]
RewriteRule ^home/?$ main.php [L,NC]
Suggested Read: Apache mod_rewrite Introduction
Related
I have no understanding at all on how to make/modify an htaccess rewrite rule, and I havent found anything to this specific issue.
I need to change
site.com/file.php
to
site.com/folder/file
So the .php also has to go. It needs to work for all files
Come on, there are plenty of examples for that. A little research and effort would be better.
Anyway, here is how your htaccess (in root folder) should look like
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -f
RewriteCond %{THE_REQUEST} \s/([^/]+)\.php(?:\s|\?) [NC]
RewriteRule ^ /folder/%1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^folder/([^/]+)$ /$1.php [L]
So, I'm currently writing my .htaccess file, and I've stumbled upon the issue of root folders. Currently, http://localhost/public_html/index is the root of my website. What I want to do is have the url as http://localhost/index instead.
I know this is pretty simple, but all of the posts I have looked at so far have not proven to work. Here is what I have so far, which is producing a 404 error.
RewriteEngine On
RewriteCond ${REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
RewriteCond %{REQUEST_URI} !^/public_html/
RewriteRule ^(.*)$ /public_html/$1 [QSA]
Please include any documentation or guides to .htaccess itself if possible.
Thanks.
The following .htaccess code can be added to achieve the results wanted:
RewriteEngine on
RewriteCond %{REQUEST_URI} !public_html/
RewriteRule (.*) /public_html/$1 [L]
I have a setup that sets variables for the index page, but it also does the same for directories and I don't want it to do that. Here's my .htaccess file:
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/(login|images|favicon\.ico|home|about|sitemap|contactus|termsandconditions|privacypolicy|signup|search|careers|error|css|js) [NC]
RewriteRule ^([a-zA-Z0-9]+)$ index.php?name=$1
RewriteRule ^([a-zA-Z]+)/$ index.php?name=$1
RewriteRule ^([a-zA-Z]+)/([a-zA-Z0-9_]+)$ index.php?name=$1&page=$2
RewriteRule ^([a-zA-Z]+)/([a-zA-Z0-9_]+)/$ index.php?name=$1&page=$2
RewriteRule ^php/$ error/
Now, with this setup, if I type in mysite.com/login it will redirect to the index.php page and set login as the name variable. How do I make it to where it ignores the directories? This is frustrating me and I've looked through this site for over an hour for an answer and can't find a similar question (I might suck at that too, though. haha!).
Also, if you look at the last RewriteRule, you can see that I'm trying to redirect any attempt to access my php/ folder to my error/ folder. This is also not working.
RewriteCond only applies to the immediately following RewriteRule. Also, you can combine lines 3&4, and 5&6 respectively, by using /? on the end, which makes the / optional (regex).
Your file could be similar to this:
RewriteEngine On
#the following line will not rewrite to anything because of "-", & stop rewiting with [L]
RewriteRule ^(login|images|favicon\.ico|home|about|sitemap|contactus|termsandconditions|privacypolicy|signup|search|careers|error|css|js)/?(.*)$ - [L,NC]
RewriteRule ^php/?(.*)$ error/ [L,NC]
RewriteRule ^([^/]+)/?$ index.php?name=$1 [L]
RewriteRule ^([^/]+)/([^/]+)/?$ index.php?name=$1&page=$2 [L]
You may be interested in the Apache Mod_Rewrite Documentation.
RewriteCond %{REQUEST_URI} !^/(login|images|favicon\.ico|home|about|sitemap|contactus|termsandconditions|privacypolicy|signup|search|careers|error|css|js) [NC] !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
to either not execute the rule on a directory or a file
More info on the mod_rewrite documentation pages, search for CondPattern
Let me preface by saying that I'm fairly new to .htaccess authoring and have usually left the nitty gritty up to my hosting provider. I've recently decided to implement a hand rolled MVC framework in php and was using the .htaccess file to redirect using "seo" friendly urls. My MVC implementation uses the variables module, class, event, and parameter. They are specified in the url in that order, e.g. http://mydomain.com/module/class/event/parameter. I want it to work if you chop off any part of the url. This was all working fine until I moved my site up one level and subsequently copied my .htaccess file. Now I'm getting an infinite redirect loop with one of the rules that was working fine before I moved it.
Here is my .htaccess file:
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{DOCUMENT_ROOT}/%{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/%{REQUEST_FILENAME} !-f
RewriteRule ^$ /index.php?module=default&class=home [QSA,L]
RewriteCond %{DOCUMENT_ROOT}/%{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/%{REQUEST_FILENAME} !-f
RewriteRule ^([^/]*)$ /index.php?module=$1 [QSA,L]
RewriteCond %{DOCUMENT_ROOT}/%{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/%{REQUEST_FILENAME} !-f
RewriteRule ^([^/]*)/([^/]*)/([^/]*)$ \
/index.php?module=$1&class=$2&event=$3 [QSA,L]
RewriteCond %{DOCUMENT_ROOT}/%{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/%{REQUEST_FILENAME} !-f
RewriteRule ^([^/]*)/([^/]*)/([^/]*)/([^/]*)$ \
/index.php?module=$1&class=$2&event=$3¶meter=$4 [QSA,L]
Now, if I take out the section that reads:
RewriteCond %{DOCUMENT_ROOT}/%{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/%{REQUEST_FILENAME} !-f
RewriteRule ^([^/]*)$ /index.php?module=$1 [QSA,L]
It works great. The problem is if I leave that out and someone types http://mydomain.com/module I get a 404 (since that directory doesn't exist and none of the rules are matching). So why does that rule now not work and why did it work when index.php (and this .htaccess file) are in a sub-directory?
Well it looks like the php router solution is the best. Thanks to #prodigitalson for the heads up.
Here is the simplified .htaccess I am now using
RewriteEngine On
# Disable rewriting for existing files or directories
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# redirect all other requests to index.php
RewriteRule ^.*$ index.php [PT,L]
RewriteRule ^$ index.php [PT,L]
And this "magic" line of code does all the heavy lifting that the .htaccess was doing before:
$url = explode('/', trim($_SERVER['REQUEST_URI'], '/'));
Now I can assign my module, class, event, and parameter values from the $url array. It also solves a problem I had where I may need more than one parameter to an event. I didn't want to have to keep adding rules to the .htaccess file for each level. Now I can supply an arbitrary number of parameters and deal with the routing logic in code.
Sorry I couldn't come up with a better title.
Here is my htaccess
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)$ $1.php [L,QSA]
And this is my problem:
localhost/validpage gives me the contents of localhost/validpage.php.
localhost/validpage/blah also gives me the same (validpage.php) page and so does localhost/validpage/blah/blah/...
Therefore the problem I am facing is link duplicity(in my words!).
How do I allow localhost/validpage.php to be accessed from localhost/validpage only and nothing else, not even localhost/validpage.php.
I have started a question on *Server****Fault*** too but with not much success.
The answer I have got is it cannot be done with htaccess alone.
By validpage I mean any valid page on the server. Since I am retrofitting an existing site with mod_rewrite for cleaner urls, I am looking for a relatively easy solution preferably with .htaccess only. However, any solutions are welcome.
what is the source attribute of your images, etc ???
absolute or relative?
<img src="/images/my.jpg" /> and <img src="images/my.jpg" /> point to different files when applying your rewrite rules.
You could try using this in your .htaccess
RewriteEngine on
RewriteBase /
# if request has a php extension remove and redirect
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{REQUEST_FILENAME} -f
RewriteCond %{REQUEST_URI} ^((.*)\.php)$
RewriteRule ^(.*).php$ $1 [L,R=301]
# if request uri has no extension link to php file
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^\.]+)$ $1.php [NC,L]
I should rewrite your php scripts to friendly urls, and redirect requests using the .php extension.