Rewrite Rules to just add .php not working - php

So I have a few files like
about.php
contact.php etc
I'm trying to set up rewrite rules so that I can access them through /about and /contact
To do this I've got the following:
RewriteEngine on
RewriteRule ^(.+)$ $1.php
But it doesn't seem to be working. Is there something I'm doing wrong?

This is my simple rewrite rule to rewrite only if the file does not exist.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(.*) $1.php
If its not wirking you should check if you have set AllowOverride all in your vhost configuration. Otherwise the rewrite rule is not working.

Related

How to use .htaccess rewrite to completely rewrite the URL

I'm trying to imitate a routing effect. In my setup the login.php file is located under localhost/session/login.php. I've already found out how to omit the .php file ending but that's not enough for me. Here is my .htaccess file:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
This changes the URL to localhost/session/login but what I really need it an URL like this: localhost/login.. The trouble is that I don't really understand how the rewrite above works and I'm getting a lot of mixed answers.. don't know what to do exactly.
Is there a way to have my URL be localhost/login by using some RewriteCondition?
You may use following rules in your site domain .htaccess to enable use example.com/login rewriting to example.com/session/login.php:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/session/$1.php -f
RewriteRule ^(.+?)/?$ session/$1.php [L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^(.+)/?$ $1.php [L]
Enable mod_rewrite and .htaccess through httpd.conf and then put this code in your .htaccess.
Make sure to put this .htaccess in DOCUMENT_ROOT directory.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /session/$1 [L]
</IfModule>
Flags used are,
L - Last
This will serve the file at location localhost/session/login.php to localhost/login.php

The Page 404 not working with .htaccess?

i have this Code in my .htaccess file:
#Rewrite everything to https
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1%{REQUEST_URI} [R=301,QSA,NC,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+?)/?$ index.php?witz=$1 [L,QSA]
ErrorDocument 404 https://example.com/erorrpage.php
ErrorDocument 403 https://example.com/erorrpage.php
i have one argument like this:
https://example.com/var
Your ErrorDocument is not getting triggered because of the following Rule you have in your htaccess
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+?)/?$ index.php?witz=$1 [L,QSA]
The Rule redirects everything that doesn't exist as directory !-d and !-f file to /index.php . Since mod-rewrite Rules are applied before mod-core your ErrorDocument will never get applied.
To solve this,You need to use a mod-rewrite Rule instead of ErrorDocument to Redirect non-existent requests to 404 page. So put the following at the top of your htaccess
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+?)/?$ - [R=404,L]
If you want to redirect 404 requests to a specific page ,you can use the following
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+?)/?$ /errorpage.php [R= 404,L]
I will suppose that you use apache.
Ensure that your .htaccess file are on the right directory (like root directory for example) or erorrpage.php is accessible on the https://example.com/
Try to simplify it with ErrorDocument 404 "Custom very simple 404 error", just to see if it works
Ensure that your mod rewrite is enabled (on httpd.conf, uncomment the ;LoadModule rewrite_module modules/mod_rewrite.so if not. Then restart apache)
Find the relevant directory tag for your www root (on other words, on your apache config of your root directory) and change AllowOverride None to AllowOverride All
If the problem occured in a web hosting, the ensure that it allow the .htaccess
If all of these does not work, try to check apache official docs

Friendly Url don't get the link [duplicate]

I am just new to .htaccess.
I need some rewrite rules for URLs.
I Google'd some and applied but no change in URL.
I want:
demo.example.com/section.php?id=1
Changed to:
demo.example.com/section/sample-section
i tried
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^section/(\d+)*$ ./section.php?id=$1
but no difference
Thanks.
I will appreciate your help.
First, make sure mod_rewrite is enabled and htaccess files allowed in your Apache configuration.
Then, put this code in your htaccess (which has to be in root folder)
Options -MultiViews
RewriteEngine On
# redirect "/section.php?id=xxx" to "/section/xxx"
RewriteCond %{THE_REQUEST} \s/section\.php\?id=([0-9]+)\s [NC]
RewriteRule ^ /section/%1? [R=301,L]
# internally rewrite "/section/xxx" to "/section.php?id=xxx"
RewriteRule ^section/([0-9]+)$ /section.php?id=$1 [L]
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^section/([^/]+)$ /section.php?id=$1 [L]
This will turn example.com/section.php?id=X to example.com/section/X
I suggest storing the URI in a database then using section.php?uri=
For example:
example.com/section.php?uri=super-awesome
would turn into:
example.com/section/super-awesome

htaccess Ignoring directory and its subdirectories

I have the following in my htaccess file:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(mailinglist)/.*$ - [L]
RewriteRule ^(.*)$ index.php?url=$1 [QSA,L]
</IfModule>
I basically want to remove htaccess from hitting that last line if I am in the mailinglist directory.
This only works for items in the root of the /mailinglist directory. Once I go deeper like /mailinglist/w/1 it breaks and hits that last rewrite rule. How do I stop it from processing that last rewrite rule if I am in the /mailinglist directory.
The reason is I have a different set of htaccess in that directory and I do not want this htaccess to control it.
Try:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !^(mailinglist)/.*$
RewriteRule ^(.*)$ index.php?url=$1 [QSA,L]
</IfModule>
I just switched the checking for mailinglist to be a RewriteCond. The condition will only rewrite to index.php if the URI doesn't begin with mailinglist.
The conditions are being applied to the wrong rule, You need to swap around your rules:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^(mailinglist)/.*$ - [L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?url=$1 [QSA,L]
</IfModule>
Conditions only apply to the immediately following rule, so the 2 !-f and !-d conditions are being misapplied to the passthrough, while the index.php rule is missing those conditions.

apache rewrite rule

I have problems with apache mod rewrite
I have this code in my .htaccess :
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?route=$1 [QSA,L]
to rewrite routes like this
sitename.com/index.php?foo/bar/baz
to this
sitename.com/foo/bar/baz
It works fine but I have problems with css|js|jpg|png and all other files
how can I exclude rewriting rules for specific types of files?
You just need an additional rewrite condition which tests for that kind of filename:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !/\.(css|js|jpg|png)$/i
RewriteRule ^(.*)$ index.php?route=$1 [QSA,L]

Categories