mod_rewrite is not working with my urls - php

I am trying to make SEO friendly URLs of using apache mod_rewrite.
My normal URLs is something like this -
index.php?p=about
index.php?p=contact
index.php?p=this
My expecting SEO friendly URLs should be something similar to this -
localhost/php_advance/ch02/about
localhost/php_advance/ch02/contact
localhost/php_advance/ch02/this
I tried it with creating a .htaccess file and doing some changes to my apache httpd.conf file.
This is my .htaccess file
<ifModule mod_rewrite.c>
# Turn on the engine:
RewriteEngine on
# Set the base to this directory:
RewriteBase /php_advance/ch02/
# Redirect certain paths to index.php:
RewriteRule ^(about|contact|this|that|search)/?$ index.php?p=$1
</ifModule>
And also, At the end of the httpd.conf file, I added some code like this -
<Directory "C:/wamp/www/php_advance/ch02">
AllowOverride All
</Directory>
NOTE: I am using WAMP server and Windows 07
But this coding is not working for me. Hope someone will help me out.
Thank you.

Make sure mod_rewrite is turned on. In your httpd.conf file, you should have something similar to:
LoadModule rewrite_module modules/mod_rewrite.so
Then make sure your htaccess file is in your /ch02/ directory.

Try this:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /php_advance/ch02/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /php_advance/ch02/index.php [L]
</IfModule>

Related

Using mod_rewrite() on 1and1 shared hosting

Update
I couldn't get this to work adding rules to the .htaccess file, in the end I used the Wordpress Rewrite class and added an add_rewrite_rule() to do the re-write which worked perfectly.
Original Post
I have been trying to put together a .htaccess rule to move parts of the path to a query string. As per https://wiki.apache.org/httpd/RewritePathInfo.
I have two folders in the root of my webspace, one for www.mydomain.com and one for test.mydomain.com. My .htaccess file is in the test.mydomain.com folder, and I want to rewrite test.mydomain.com/images/parm1/parm2 to test.mydomain.com/images/?gwpw=parm2&gwpi=parm1.
If I put garbage in the .htaccess file I get a 500 internal server error so I know the file is being read. I also have some existing rules in .htaccess from Wordpress that use mod_rewrite(), which if I comment out, stop the site redirecting to a 404 error page if you try to access test.mydomain.com/somegarbage. So I know mod_rewrite() is enabled.
I have read I need to set Options +FollowSymLinks -MultiViews, so the whole rule I have is:
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /
RewriteRule ^/images/([^/]*)/([^/]*) /images/?gwpw=$1&gwpi=$2 [PT]
I have tried this in different parts of the file, removing the leading slash from /images/?gwpw=.... I've tried adding a rule that I thought should rewrite every request to a different page like RewriteRule ^/* /somewhere but I can't seem to get any rewrite to work.
As this is a shared server I can't access httpd.conf.
My entire .htaccess file currently looks like this:
<IfModule php5_module>
php_flag session.cookie_httponly on
</IfModule>
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /
RewriteRule ^/images/([^/]*)/([^/]*) /images/?gwpw=$1&gwpi=$2 [PT]
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
# Wordfence WAF
<Files ".user.ini">
<IfModule mod_authz_core.c>
Require all denied
</IfModule>
<IfModule !mod_authz_core.c>
Order deny,allow
Deny from all
</IfModule>
</Files>
I am a complete newbie when it comes to .htaccess rules, so any help would be greatly appreciated. I'm clearly doing something wrong but I can't see the wood for the trees anymore!

.htaccess redirects to a template selection php file

How would I configure my .htaccess file if I want all requests to always load a core template selection file. I believe this could be good for a few reasons the first of which being security. Only if my php template selection code flags a file as safe for display will it display. Also, it allows me to build some interesting cms functionality like requiring a php metadata array in order to load the template file. How could I accomplish this?
Also, do you think it is practical to choose this method over others?
You can use this rule in your root .htaccess to make core/template.php your front controller:
RewriteEngine on
RewriteBase /
RewriteRule ^core/template\.php$ - [L,NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . core/template.php [L]
EDIT: If you want even real files to be routed to same controller use last rule as:
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule !\.(jpe?g|ico|gif|bmp|png|tiff|css|js)$ core/template.php [L,NC]
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /
RewriteRule ^(.*)$ /index.php
</IfModule>
Make sure that inside the httpd.conf this line isn't commented:
LoadModule rewrite_module modules/mod_rewrite.so
(It should be without the '#' at the beginning)
And this is my directory tag (I'm using wamp but it should be the same, and of course restart the apache server if you changed something there)
<Directory />
Options FollowSymLinks
AllowOverride All
Order deny,allow
Allow from all
</Directory>

.htaccess file in XAMPP is not working on windows 7

I have the following URL on my localhost:
http://localhost/?cmd=causeListByCourtName
http://localhost/?cmd=here could be any other page name
I have tried to rewrite the URL like =
http://localhost/page/causeListByCourtName
I have tried this:
RewriteEngine On
RewriteRule ^pages/(.+)/?$ .+/?cmd=$1 [NC,L]
# Handle pages requests
But it do nothing. I am using XAMPP on my windows 7.
in my httpd.conf :
LoadModule rewrite_module modules/mod_rewrite.so
is already enabled. What I am doing wrong?
You need to also make sure you changed AllowOverride None to AllowOverride All in your httpd.conf file wherever you find it.
Try doing it this way.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^page/(.+)/?$ /?cmd=$1 [NC,L]
Options +FollowSymlinks
RewriteEngine on
RewriteRule ^page/(.*)$ index.php?cmd=$1 [L]
This in case you use index.php as your default page. You can cut down the index.php from the front

Explanation of this .htaccess file

I have recently added a .htaccess file to my website root in order to achieve something I have already asked in this question:
Create blog post links similar to a folder structure
Now, the top answer was brilliant and it worked on localhost, however on my server it doesn't.
To be honest with you, I am not quite sure what these statements in the .htaccess mean exactly. I would be grateful if somebody could explain them line by line.
So this is what I have
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
<IfModule mod_rewrite.c> # If the module for rewrite is available
RewriteEngine On # turn it on
RewriteBase / # set / to the relative root path
RewriteRule ^index\.php$ - [L] # don't rewrite index.php and stop the rest of the rules
RewriteCond %{REQUEST_FILENAME} !-f # if file does not exist
RewriteCond %{REQUEST_FILENAME} !-d # and file is not a directory
RewriteRule . /index.php [L] # show the index page instead
</IfModule> # close the IfModule clause
I suspect if it doesn't work on your server, that the root directory isn't set properly for the domain or server configuration. You'll wanna look into where the files are placed and how the httpd.conf is configured to use those paths.
Also, for next time Apache has a wonderful documentation on this module that should cover everything I've just said, in more depth.
The actual problem was that the rewrite was not enabled on my virtualhost.
So I enabled it in my virtualhosts and everything works now.
<VirtualHost *>
ServerName mysite.com
DocumentRoot /var/www/html/mysite.com
<Directory /var/www/html/mysite.com>
Options FollowSymLinks
AllowOverride all
</Directory>
</VirtualHost>

Simple problem with mod_rewrite in the Fat Free Framework

I am trying to setup and learn the Fat Free Framework for PHP.
http://fatfree.sourceforge.net/
It's is fairly simple to setup and I am running it on my machine using MAMP.
I was able to get the 'hello world' example running just fin:
require_once 'path/to/F3.php';
F3::route('GET /','home');
function home() {
echo 'Hello, world!';
}
F3::run();
But when I try to add in the second part, which has two routes:
require_once 'F3/F3.php';
F3::route('GET /','home');
function home() {
echo 'Hello, world!';
}
F3::route('GET /about','about');
function about()
{
echo 'About Us.';
}
F3::run();
I get a 404 error if I try the second URL: /about
Not sure why one of the mod_rewrite commands would be working and not the other.
Below is my .htaccess file:
# Enable rewrite engine and route requests to framework
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-l
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php [L,QSA]
# Disable ETags
Header Unset ETag
FileETag none
# Default expires header if none specified (stay in browser cache for 7 days)
<IfModule mod_expires.c>
ExpiresActive On
ExpiresDefault A604800
</IfModule>
So my friend actually helped me out with this issue. I ran into the exact same problem, but basically I'm using MAMP also and have all my Fat Free files within a fatfree dir within htdocs of MAMP.
The solution is you need to mod the RewriteBase to point to /[dirname]/ instead of just / and then change RewriteRule to /[dirname]/index.php.
My .htaccess looks like this:
RewriteEngine On
RewriteBase /fatfree/
RewriteCond %{REQUEST_FILENAME} !-l
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* /fatfree/index.php [L,QSA]
After that's set, you can follow the example in the Fat Free doc exactly and it'll work like a charm. This stumped me for a while and this was all it needed. Also, if you're using MAMP, edit the httpd.conf file in /Applications/MAMP/conf/apache and be sure to alter the following:
<Directory />
Options Indexes FollowSymLinks
AllowOverride None
</Directory>
to
<Directory />
Options Indexes FollowSymLinks
AllowOverride All
</Directory>
Basically change None to All.
If you're running F3 under a subfolder, you must change the RewriteBase in .htaccess to match the folder.
In your .htaccess you have 'index.php' it needs a slash ... '/index.php'
# Enable rewrite engine and route requests to framework
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-l
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* /index.php [L,QSA]
otherwise when it tries to rewrite /about/ it will look for /about/index.php instead of just the root /index.php
I just had another thought.. it 'is' possible that althought mod_rewrite is intalled there may be a quirk with the server causing it not to rewrite..
If the global route below doesnt work you might want to test the rewrite
RewriteRule ^/google http://www.google.com [L,NC];
You could also try a global route for the directory
F3::route('GET /about/*','about');
but that means anythin under domain.com/about/ ...... anything ... will reroute to the about function...
A note about mod_rewrite and FF
As you said, FF is givikng you a 404 because it is expecting '/' instead of '/index.php'... However, it is the index.php which is expecting the difference..
To demonstrate that, i believe you can duplicate your
F3::route('GET /','home');
as
F3::route('GET /index.php','home');
and the page should display...
The reason for this is if you just go to the / directory (or /index.php) eitehr way apache servesx the index.php page....
The mod_rewrite allows you to redirect the /about and have it redirect to the index.php.. So if your rewrite rule is not working then the redirect/rewrite does not happen and you will get a 404...
As i mentioned above, test the mod_rewrite with the google rule.. then try to go to http://localhost:80/google
if it does not redirect you to google then your rewrite engine is not working... (probably an issue with the windows configuration..)
to enable mod_rewrite under windows:
Open your http.conf
Find this line:
#LoadModule rewrite_module modules/mod_rewrite.so
remove the comment mark (#) from the line... so you have:
LoadModule rewrite_module modules/mod_rewrite.so
Save the file and restart apache..
Alternatly.. I think you can just say:
LoadModule rewrite_module modules/mod_rewrite.so
at the start of your htaccess file...
I banged my head on this for 2 days. I debugged htaccess and php both. The actual problem is this :
If you copied the .htaccess file from their fatfree-1.4.4 zip download, its not .htaccess its htaccess (. is missing) Just rename this file to .htaccess from htaccess and everything will work perfectly as its mentioned in the document !!!
I am using this .htaccess works for non-root folders too
Options -Indexes
IndexIgnore *
RewriteEngine On
RewriteCond $1 !^(index\.php|images|main\.css|form\.css|favicon\.ico|robots\.txt|sitemap\.xml)
RewriteRule ^(.*)$ index.php/$1 [L,QSA]
Note: If you want to call your .htaccess file something else, you can change the name of the file using the AccessFileName directive. For example, if you would rather call the file .config then you can put the following in your server configuration file:
AccessFileName .config
here
This response may be too late for you but I had the same problem yesterday.
It sounds like the problem is apache is not rewriting urls. I had the same issue when trying to get F3 running on OSX 10.7 - the 'GET /' route would work but not the 'GET /foo' as the F3 index.php was in a subdir for localhost/F3. My solution was to:
ensure the .htaccess was set as you have.
ensure mod_rewrite.so was enabled in apache's httpd.conf
ensure that you set AllowOverride All (mine was None) for your web directory in httpd.conf (further down the file).
restart apache
Without step 3, apache will ignore any rewrite directives. I discovered this by changing the permalinks on a local wordpress install and they failed indicating the problem was the apache config, not F3.
I'm running this with a MAMP stack.
I renamed their folder from "fatfree-master" to "f3".
I put that folder next to htdocs.
Their .htaccess file (now inside MAMP/f3/lib) remains untouched.
My .htaccess (in my web subfolder) is stock standard as per their example:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule .* index.php [L,QSA]
Hope this helps someone.
I had the same issue and i solved it by moving the .htaccess file to the root directory of the fatfree project.
create a file .htaccess with folowing code at root directory
# Enable rewrite engine and route requests to framework
RewriteEngine On
# Some servers require you to specify the `RewriteBase` directive
# In such cases, it should be the path (relative to the document root)
# containing this .htaccess file
#
# RewriteBase /
RewriteCond %{REQUEST_URI} \.ini$
RewriteRule \.ini$ - [R=404]
RewriteCond %{REQUEST_FILENAME} !-l
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php [L,QSA]
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization},L]

Categories