redirect and rewrite url using .htaccess - php

Recently I was thinking of making some seo friendly urls by rewrite and redirecting urls.
the rule which I wrote is working fine for this type of url like xyz.com/abc.php this automatically got converted into xyz.com/abc now I need help with this type of urls abc.com/study.php/abc/123 which I want to redirect on abc.com/study/abc/123
my .htaccess file is
RewriteEngine On
# browser requests PHP
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^\ ]+)\.php
RewriteRule ^/?(.*)\.php$ /$1 [L,R=301]
# check to see if the request is for a PHP file:
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^/?(.*)$ /$1.php [L]
EDIT.
It was working fine now I have changed my mind that instead of removing .php extension from all files I want to remove .php extension specifically from 2 files i.e a.php and b.php.

You can tweak your existing rules to support this PATH_INFO as well like this:
# browser requests PHP
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\s/+(\S+?)\.php(/\S*)?\sHTTP [NC]
RewriteRule ^ /%1%2 [L,R=301,NE]
# check to see if the request is for a PHP file:
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^/?(.+?)(/.*)?$ /$1.php$2 [L]

Related

How to redirect or 'hide' the .php ending on pages

I had a static HTML website. I am now using some PHP and so my pages have the .php ending. For example www.example.com/about.php
How can I redirect or 'hide' the .php ending so that the above example becomes www.example.com/about ?
I have a .htaccess file for my old website. Code below. But I don't know how to edit it so that it applies to my new .php pages.
RewriteEngine On
# add www and turn on https in same rule
RewriteCond %{HTTP_HOST} !^www\. [NC,OR]
RewriteCond %{HTTPS} !on
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^ https://www.%1%{REQUEST_URI} [R=301,L,NE]
## hide .html extension
# To externally redirect /file.html to /file
RewriteCond %{THE_REQUEST} \s/+(.+?)\.html[\s?] [NC]
RewriteRule ^ /%1 [R=301,NE,L]
# To internally rewrite /file to /file.html
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^(.+?)/?$ $1.html [L]
The first bit about adding www and turn on https I'd like to keep.
The second bit about hiding .html extension needs to be changed.
The last bit I don't think I ever needed and can be deleted. I don't think I need to rewrite any internal links from /file to /file.php
Many thanks
You can use the same commands that are used for html in your present .htaccess. Simply replace html by php.
Hence,
...
## hide .html extension
# To externally redirect /file.html to /file
RewriteCond %{THE_REQUEST} \s/+(.+?)\.html[\s?] [NC]
RewriteRule ^ /%1 [R=301,NE,L]
# To internally rewrite /file to /file.html
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^(.+?)/?$ $1.html [L]
would become
...
## hide .php extension
# To externally redirect /file.php to /file
RewriteCond %{THE_REQUEST} \s/+(.+?)\.php[\s?] [NC] # html --> php
RewriteRule ^ /%1 [R=301,NE,L]
# To internally rewrite /file to /file.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f # html --> php
RewriteRule ^(.+?)/?$ $1.php [L] # html --> php
in your .htaccess.
Obviously, this will disable the 'functionnality' for .html files.
It sounds like when someone visits www.example.com/about.php you have a file on your server at the root level to handle it called about.php. And if someone visits another page like www.example.com/another-page.php you would have a separate php file saved at the root level called another-page.php to handle that request.
If that's what you're trying to do, then I think you can just add one conditional statement to the htaccess file that rewrites not only .html but also .php to remove the endings, like this:
RewriteCond %{THE_REQUEST} \s/+(.+?)\.php [NC]
But if you are doing what I think you're doing on the backend, you will still need the last part for internal file mapping. The middle part basically says, if someone externally requests www.example.com/about.php send them away with an 301 code telling them to try again at www.example.com/about instead. So browsers will kindly abide by your direction and come back with a second request to www.example.com/about. However, on the back end, your server needs to know what to do with the request to /about. If you want the file called about.php to handle it, you need to tell the server that with the following lines:
# To internally rewrite /about to /about.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+?)/?$ $1.php[L]
Here is your edited .htaccess file for completenes:
RewriteEngine On
# add www and turn on https in same rule
RewriteCond %{HTTP_HOST} !^www\. [NC,OR]
RewriteCond %{HTTPS} !on
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^ https://www.%1%{REQUEST_URI} [R=301,L,NE]
## hide .html and .php extension
# To externally redirect /file.html to /file
RewriteCond %{THE_REQUEST} \s/+(.+?)\.php [NC]
RewriteCond %{THE_REQUEST} \s/+(.+?)\.html[\s?] [NC]
RewriteRule ^ /%1 [R=301,NE,L]
# To internally rewrite /file to /file.html
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+?)/?$ $1.html [L]
Most php frameworks send all their traffic to one entry point, for example /index.php and work out the routing based on the url. In other words, the frameworks tell the server, don't worry about trying to route requests to the right files, just give them all to me at index.php and I'll figure it out from there. Of course, you need to have logic that determines if the url is valid, and if so grabs the right file and echos it out once found. For example, in that situation, if someone came to your site with a request to something.php your back end framework would send a 301 code saying, "sorry, try again without the php next time".

how to change php extension into aspx extension using htaccess

I'm new on php programming and i'm customizing an CRM base on PHP. I want to change the .php extension in .aspx, whether the php script is in the root file or sub directory, the .aspx should show up.
Here is my URL
I want to change THIS:
http://localhost/edev/pages/UI.php?operation=req
Into:
http://localhost/edev/pages/UI.aspx?operation=req
the code that i have tried
.HTACCESS
RewriteEngine On
# Unless directory, remove trailing slash
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/$ http://localhost/test/$1 [R=301,L]
# Redirect external .php requests to extensionless url
RewriteCond %{THE_REQUEST} ^(.+)\.php([#?][^\ ]*)?\ HTTP/
RewriteRule ^(.+)\.php$ http://localhost/test/$1 [R=301,L]
# Resolve .php file for extensionless php urls
RewriteRule ^([^/.]+)$ $1.php [L]
Let me know if you have any question according to this question. Thank You :)
Try this:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)\.aspx$ $1.php [L]

HTACCESS rules redirection error

In my htaccess file I have added a rule to redirect url/abc/xyz to url/abc/xyz.php so now even when I try to access url/abc.php it redirects me to url/abc/
Help me with this.. My code now :
# To externally redirect /dir/foo.php to /dir/foo
RewriteCond %{THE_REQUEST} ^GET\s([^.]+)\.php\s [NC]
RewriteRule ^ %1 [R,L]
## To internally forward /dir/foo to /dir/foo.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteCond %{QUERY_STRING} ^$
RewriteRule ^(.*?)/?$ $1.php [L]
What I want is :
url/abc.php & url/abc should goto url/abc.php
url/abc.php/#x & url/abc/#x should goto url/abc.php/#x
If I've interpreted your intentions correctly, the following rewrite rules should serve your wishes:
# Enable rewriting
RewriteEngine On
# Define site root
RewriteBase /
# Do not rewrite for image, css or js files
RewriteRule \.(css|jpe?g|gif|png|js)$ - [L]
# Add index.php to requests ending with a trailing slash
RewriteRule ^([a-z0-9_\-/]*/)?$ $1index.php [NC,R=301,L]
# Add .php to the end of a request without a trailing slash
RewriteRule ^(([a-z0-9_\-]+/)*[a-z0-9\-]+)$ $1.php [NC,R=301,L]
What the rules do:
Rewriting happens relatively to the root (/) of the domain
Rewrite rules will ignore any file with a file extension listed in rule nr 2.
Requests for http://example.com/ (or http://example.com), http://example.com/foo/ and http://example.com/foo/bar/ will be rewritten to http://example.com/index.php, http://example.com/foo/index.php and http://example.com/foo/bar/index.php respectively
Requests for http://example.com/baz and http://example.com/baz/boo will be rewritten to http://example.com/baz.php and http://example.com/baz/boo.php respectively
The URL fragment (#) is never sent to the server so you cannot use mod_rewrite to redirect it. In other words the # is only interpreted in the browser so you shouldn't need to worry about it in .htaccess.
This code should work for you, or at least get you started:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
</IfModule>
I have also enclosed it in an IfModule to check to see if mod_rewrite is enabled first.
My preferred way, however, is to redirect everything to index.php and then have PHP code there that will break up the URLs and redirect to the appropriate pages. This works really well with Model-View-Controller (MVC) systems and other similar variants. This also allows general site settings to all be loaded from one place rather than many places. Here is the code that I would use for this type of setup:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_URI} !^/img/[0-9a-zA-Z_\-]+\.(gif|jpg|png)$
RewriteCond %{REQUEST_URI} !^/css/[0-9a-zA-Z_\-]+\.css$
RewriteCond %{REQUEST_URI} !^/js/[0-9a-zA-Z_\-]+\.js$
RewriteRule ^.+$ index.php [L]
</IfModule>
The first three lines says to not redirect any images, css, or js files in the specified directories.

.htaccess mod_rewrite php extension and get querystring

I have the following in my .htaccess which basically removes .php extensions from url and redirects user to the url without the .php extension. However, if i have a querystring for lets say account.php, it will be account?user=john&id=12, I would like my htacess code below to be abe to let convert the querystring to forwardslashes.
Any ideas how I can do this? Thanks
RewriteEngine On
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^\ ]+)\.php
RewriteRule ^/?(.*)\.php$ /$1 [L,R=301]
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^/?(.*)$ /$1.php [L]
Create a .htaccess file with the code below
RewriteEngine on
RewriteRule product-categoryid-(.*)-productid-(.*)\.htm$ Your Root URl?categoryid=$1&productid=$2
Your Root URl
Root URL means like wwww.google.com/

.htaccess replace .php with / for every PHP file

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}.

Categories