I want to rewrite url on my site such that,
www.example.com/login points to www.example.com/login.php
www.example.com/login/ too points to www.example.com/login.php
Pages deep inside the site should follow the above rule. i.e
www.example.com/inner/inner2/something
and www.example.com/inner/inner2/something/ should also point to
www.example.com/inner/inner2/inner3/something.php
www.example.com/login/anything , if doesn't exist should show 404 (as in my case it's causing loops)
The rule I have written is:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*[^/])$ $1.php [L]
RewriteRule ^(.+)/$ $1.php [L]
It works fine, in 3 of the above cases but in 4th it's causing loops.
Also I want to show an 404, if a file with .php extension is requested.
And also, the behaviour of above rule changes if I use REQUEST_URI instead of REQUEST_FILENAME. What's the difference between the two?
TIA
Try this rule for php extension hiding:
# To externally redirect /dir/file.php to /dir/file
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+(.+?)\.php[\s?] [NC]
RewriteRule ^ /%1 [R=301,L,NE]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^(.+?)/?$ /$1.php [L]
Related
I've been trying to rewrite a specific file I have, /u.php/example to just /u/example with htaccess. The /example part I will get and use through php url parsing.
So, another question I read suggested using the following code
RewriteCond %{THE_REQUEST} /(u|a)\.php [NC]
RewriteRule ^ /%1 [L,R]
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*?)/?$ /$1.php [L]
But that does not preserve the /example from /u.php/example when rewritten, and causes a 500 error if I just enter /u/example.
Is there a work around for this? What could be the problem? I haven't been able to find a solution on Stack.
You need to capture and redirect the /example part from URI
RewriteCond %{THE_REQUEST} /(u|a)\.php/([^\s]*) [NC]
RewriteRule ^ /%1/%2 [L,R]
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*?)/(.*)?$ /$1.php/$2 [L]
I'm trying to get the following functionality to work in PHP without a framework. I don't want to have to worry about setting up a super complicated framework for every PHP application I do.
http://domain.com/sign_up.php
becomes
http://domain.com/sign_up/
http://domain.com/user.php?id=432
becomes
http://domain.com/user/?id=432
Or if there is a way to get that to become http://domain.com/user/432 but i'm not sure how to handle multiple $_GET variables in that scenario so that's optional.
This works pretty well so far:
RewriteRule ^sign_up/([^/]*)$ /sign_up.php?p=$1 [L]
The only problem is I have to do that for every single php file i'm using which can become a lot.
What is a universal way to do it for all php files?
UPDATES:
This one line is working perfectly:
RewriteRule ^(.*)\/$ $1.php [NC]
Only issue is it doesn't auto redirect PHP
For example, I want to 301 auto redirect:
http://domain.com/file.php
to
http://domain.com/file/
And
http://domain.com/file.php?var1=value&var2=value
to
http://domain.com/file/?var1=value&var2=value
If anyone can think of a better way to handle query string values in a more SEO friendly way that would be awesome! But otherwise this is working pretty great so far.
MORE UPDATES:
Now this is working:
http://domain.com/file/ - to -
http://domain.com/file.php
Both of those point to the same page with this htaccess code:
RewriteRule ^(.*)\/$ $1.php [NC]
However http://domain.com/file without the trailing / returns a page not found error.
Also I need to know how to auto redirect http://domain.com/file.php to http://domain.com/file/
MOSTLY WORKING HTACCESS
This .htaccess works beautifully:
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !\.(php|html?|jpg|gif)$
RewriteRule ^(.*)([^/])$ http://%{HTTP_HOST}/$1$2/ [L,R=301]
RewriteRule ^(.*)\/$ $1.php [NC]
The only thing it doesn't do is auto redirect if they go directly to http://domain.com/file.php it does not redirect to http://domain.com/file/ but everything else about it is working.
Try this:
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d # not a dir
RewriteCond %{REQUEST_FILENAME} !-f # not a file
RewriteCond %{DOCUMENT_ROOT}/$1.php -f # but php exists
RewriteRule ^([^/]+)/([^/]+)?$ $1.php?p=$2 [L]
However http://domain.com/file without the trailing / returns a page not found error.
That's because your rule does not match unless there's a / at the end.
RewriteRule ^(.*)\/$ $1.php [NC]
^
You can make it optional with ? as
RewriteRule ^(.*?)/?$ $1.php [L]
Note, that / does not need a \ before it. It works with or without it.
Also I need to know how to auto redirect http://domain.com/file.php to http://domain.com/file/
# Rewrite original .php request to new URLs
RewriteCond %{THE_REQUEST} \ /([^.]+)\.php [NC]
RewriteRule ^ /%1/ [R,L]
# Resolve the new URLs to .php files
RewriteRule ^(.*?)/?$ $1.php [L]
If you get this working first, we can see what we can do about the query parameters later.
Your final htaccess could look like
# Rewrite original .php request to new URLs
RewriteCond %{THE_REQUEST} \ /([^.]+)\.php [NC]
RewriteRule ^ /%1/ [R,L]
# Force a trailing / if not a file
RewriteCond %{REQUEST_URI} !\..{3,4}$
RewriteRule ^(.*)([^/])$ http://%{HTTP_HOST}/$1$2/ [L,R=301]
# Redirect to php if not an existing dir
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ $1.php [L]
You'll probably want something like this:
RewriteEngine on
RewriteBase /
# redirect with trailing parameter
RewriteRule ^([\w]+).php?p=([\w]+)$ $1/$2/ [QSA,R=301]
# redirect bare php files
RewriteRule ^([\w]+).php$ $1/ [QSA,R=301]
# make sure it's not a request to an existing file
RewriteCond %{REQUEST_FILENAME} !-f
# make sure we have a trailing slash
RewriteRule ^(.+[^/])$ $1/ [QSA,R=301]
# internally point to the right file
RewriteRule ^([^/]+)/([^/]*)/?$ $1.php?p=$2 [QSA,L]
The [R=301] appendixes redirect the browser to the new URL with a 301, moved permanently status header. That way the browser will know where to find the right url in the future, without asking the server.
Also, sometimes an .htaccess checker is useful: http://htaccess.madewithlove.be/ Do note, the tool doesn't work with %{REQUEST_FILENAME}.
I need to remove the .php from all the urls on my website, and if someone types in a full url it needs to redirect to the php-extension-free version. Eg:
website.com/about.php -> website.com/about
I need to do this in as SEO friendly a manner as possible, so I'm guessing that would be a 301 redirect so Google and others know that the page has a new location at the new php-extension-free URL. Any dup content would also have to be avoided, so it's important that the page isn't accessible at both the old and new urls, which is what would happen if all I did was a simple:
RewriteRule about about.php [L]
I've seen a number of .htaccess approaches for this, but they all seem to add a trailing slash to the URL. It's important that this doesn't happen too.
Also, I have an explicit HTTPS redirect happening for a couple pages, and I need to make sure I don't create a redirect loop. Here's the code that's currently redirecting those pages to HTTPS.
RewriteCond %{HTTPS} off
RewriteRule ^(quote|quote_2).php$ https://website.com/$1.php [R=301,L,QSA]
RewriteCond %{HTTPS} on
RewriteCond %{REQUEST_URI} !^/(quote|quote_2).php
RewriteCond %{REQUEST_URI} !^/(.*)\.(css|png|js|jpe?g|gif|bmp|woff|svg|map)$
RewriteRule ^(.*)$ http://website.com/$1 [R=301,L,QSA]
You can use this code in your root .htaccess:
RewriteEngine On
RewriteCond %{THE_REQUEST} \s/+(?:index)?(.*?)\.php[\s?] [NC]
RewriteRule ^ /%1 [R=301,L,NE]
RewriteCond %{HTTPS} off
RewriteRule ^(quote|quote_2)/?$ https://%{HTTP_HOST}/$1 [R=301,L]
RewriteCond %{HTTPS} on
RewriteCond %{REQUEST_URI} !^/(quote|quote_2)
RewriteCond %{REQUEST_URI} !\.(css|png|js|jpe?g|gif|bmp|woff|svg|map)$
RewriteRule ^(.*)$ http://%{HTTP_HOST}/$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1.php -f [NC]
RewriteRule ^(.+?)/?$ /$1.php [L]
The below answer can be found here
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
I have this in my htaccess file
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
Which will allow people to access server/login.php as just server/login but how can I make it so that if server/login.php is entered into the address bar it will redirect to server/login.
My site is getting different hits rather than just for one url because of this issue, thanks for your effort and time.
You can do this with an external redirect:
RewriteRule ^(.*)\.php$ $1 [R,L]
The R-flag is a temporary redirect. If things work as you expect them to work, you can use R=301 to make it a permanent redirect. See the documentation for more information.
Edit: With your current rule, this will create an infinite loop. If you are running Apache 2.3.9 or later, use the END-flag on your internal rewrite rule. If you are unwilling or unable to use such a version of Apache, you can use the "THE_REQUEST" trick to stop an infinite loop, as this will only match if the request comes from outside:
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s(.*)\.php[\s\?] [NC]
RewriteRule ^ %1 [R,QSA,L]
Change your base path RewriteBase /path/
RewriteEngine on
RewriteBase /path/
RewriteCond %{THE_REQUEST} \.php
RewriteRule ^(.*)\.php$ $1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
I'm working in a directory called testsite and I want all .php extensions to be replaced by a trailing slash. I'm halfway there, in that (for example) entering http://mydomain.com/testsite/about means that http://mydomain.com/testsite/about.php is loaded.
However, I now want the URL to be displayed as ./about/ so that only one version shows up in search engine rankings.
Here's my .htaccess:
RewriteEngine On
RewriteBase /testsite/
RewriteRule ^()$ index.php [NC,L]
RewriteCond %{REQUEST_URI} !(^/?.*\..*$) [NC]
RewriteRule (.*)$ $1.php [NC]
Also, is it possible to preserve (and hide from display any parameters that I pass)?
All help is much appreciated!
Try these rules:
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule .*[^/]$ %{REQUEST_URI}/ [L,R=301]
RewriteRule ^$ index.php [L]
RewriteRule (.*)/$ $1.php [NC]
Try replacing the last rule with:
RewriteRule (.*)$ $1.php [NC] [E=ORIGINAL_URL:$1]