Showing posts with label htaccess. Show all posts
Showing posts with label htaccess. Show all posts

htaccess clean url php

htaccess clean url php

htaccess clean url php htaccess is the page url redirect and custom url is secure in web pages and passing data another page. follow us.

htaccess clean url php
htaccess clean url php

Redirect a dynamic redirect page

RewriteEngine On RewriteRule keyword/(.*).html keyword.php?page=$1

Page Redirect


Common variable sets

[A-Z] The variable can have uppercase letters
[a-z] The variable can have lowercase letters
[0-9] The variable can have numbers
([a-zA-Z0-9]+) The variable can have any of the above, and has no limit to the number of characters.

RewriteEngine On RewriteRule ^([a-zA-Z0-9]+)$ index.php?page=$1 RewriteRule ^([a-zA-Z0-9]+)/$ index.php?page=$1

url pass multiple variables on .htaccess

 In this post I want to explain about URLs rewriting techniques using .htaccess file. Friendly URLs looks neat on the browser address bar.



RewriteRule extraurl/(.*).html currentsite.php?id=$1 [L,QSA]

RewriteRule ^extraurl/(.*).html subayurveda.php?aid=$1 [L,QSA]

url pass multiple variable
 url pass multiple variables on .htaccess


RewriteEngine On
RewriteRule ^([a-zA-Z0-9_-]+)$ index.php?keys=$1
RewriteRule ^([a-zA-Z0-9_-]+)/$ index.php?keys=$1


<?php

$keys=$_GET['keys'];

if($keys=='home')
{
include('home.php'); // Home page
}
else if($keys=='login')
{
include('login.php'); // Login page
else if($keys=='terms')
{
include('terms.php'); // Terms page
}
else 
{
include('users.php'); // Users Gateway
}
?>

Multipul Value:

htaccess code:

RewriteRule ^([\da-z]+)/([\da-z]+)/(\d+)/?$ index.php?category=$2&language=$1&id=$3 [L,QSA,NC]
RewriteRule ^([\da-z]+)/([\da-z]+)/?$ index.php?category=$2&language=$1 [L,QSA,NC]

php code:

print($_REQUEST);
exit;

output:

category = Value and Language = Value and id






USING .HTACCESS FILES

USING .HTACCESS FILES

INTRODUCTION

The .htaccess file is a powerful tool for modifying your Apache configuration on a per-domain and even a per-directory level. Many content management systems rely on .htaccess files for configuring your site. You can also create your own .htaccess file manually.

CREATING THE .HTACCESS FILE

Your .htaccess file needs to be a plain text file. This means you need to make it in a text editing program capable of producing plain text files.
  • On a Windows machine, you can use Notepad, which is one of your built-in accessories.
  • On a Mac, you can use TextEdit. You must first open the TextEdit "Preferences" menu and, in the "New Document" section, select "Plain text." Then, you can start a new document in TextEdit for your .htaccess file.
When you save your document, make sure you name it htaccess.txt, or something similar. If you save it as .htaccess, your computer will hide the file. This is because files that start with "." are considered to be system files.
htaccess set expiry of js

htaccess set expiry of js

<IfModule mod_expires.c>
ExpiresActive On
ExpiresByType image/jpg "access 7 days"
ExpiresByType image/jpeg "access 7 days"
ExpiresByType image/gif "access 7 days"
ExpiresByType image/png "access 7 days"
ExpiresByType text/css "access 7 days"
ExpiresByType application/pdf "access 1 month"
ExpiresByType text/x-javascript "access 1 month"
ExpiresByType application/x-shockwave-flash "access 1 month"
ExpiresByType image/x-icon "access 1 year"
ExpiresDefault "access 2 days"
</IfModule>


Set Cookie using htaccess

Set Cookie using htaccess

Header set Set-Cookie "language=%{lang}e; path=/;" env=lang

Set Cookie based on Request. This code sends the Set-Cookie header to create a cookie on the client with the value of a matching item in 2nd parentheses.

RewriteEngine On
RewriteBase /

RewriteRule ^(.*)(de|es|fr|it|ja|ru|en)/$ - [co=lang:$2:.yourserver.com:7200:/]


Custom Error Pages for Better SEO

Custom Error Pages for Better SEO

One use of the .htaccess file is to redirect users to a custom error page depending on the specific web server error they encounter. By using a custom error page, you can present them with a list of your site’s top articles, a sitemap with links to the various areas of your site, and it can include your site’s navigation system. It can also feature a FAQ, so folks who are looking for information on your site, but can’t find it, are able to narrow down the location of that information on your site without leaving, going back to the search engine, and more than likely not returning to your site.

It’s not difficult to use the .htaccess file to redirect users to a custom error page–but to do it you’ll need to know the proper error code. The most common ones you’ll use are:
  • 400 – Bad request
  • 401 – Authorization Required
  • 403 – Forbidden
  • 404 – File Not Found
  • 500 – Internal Server Error
To use .htaccess with these codes, first you’ll need to open up your favorite text editor, create a new document, and in that document, specify the error message that you’ll be redirecting like this:

ErrorDocument 404 /filenotfound.html

If you wanted to redirect users for another error, such as 500, Internal Server Error, you would do it like this (and so on):

ErrorDocument 500 /servererror.html

Then you’d just save the .htaccess file (remembering to check that it is
 saved just like that, without some additional extension), and upload it
 to your web host’s root directory (or whatever directory you are 
wanting to use it in).



Implementing a Caching Scheme with .htaccess

Implementing a Caching Scheme with .htaccess

# year
<FilesMatch "\.(ico|pdf|flv|jpg|jpeg|png|gif|swf|mp3|mp4)$">
Header set Cache-Control "public"
Header set Expires "Thu, 15 Apr 2010 20:00:00 GMT"
Header unset Last-Modified
</FilesMatch>
#2 hours
<FilesMatch "\.(html|htm|xml|txt|xsl)$">
Header set Cache-Control "max-age=7200, must-revalidate"
</FilesMatch>
<FilesMatch "\.(js|css)$">
SetOutputFilter DEFLATE
Header set Expires "Thu, 15 Apr 2010 20:00:00 GMT"
</FilesMatch>
How to do htaccess redirect based on cookie value

How to do htaccess redirect based on cookie value


RewriteEngine on
RewriteCond %{HTTP_COOKIE} ^.*user_id=(\d+).*$ [NC]
RewriteRule .* http://localhost/mysite/cache/$1 [R=301,L]
RewriteRule .* http://localhost/mysite/cache/guest [R=301,L]]
RewriteEngine on
RewriteRule ^.+$ - [L]
RewriteCond %{HTTP_COOKIE} ^.*user_id=(\d+).*$ [NC]
RewriteRule .* http://localhost/mysite/$1 [R=301,L]
RewriteRule .* http://localhost/mysite/guest [R=301,L]
Custom Error Pages for Better SEO

Custom Error Pages for Better SEO

One use of the .htaccess file is to redirect users to a custom error page depending on the specific web server error they encounter. By using a custom error page, you can present them with a list of your site's top articles, a sitemap with links to the various areas of your site, and it can include your site's navigation system. It can also feature a FAQ, so folks who are looking for information on your site, but can't find it, are able to narrow down the location of that information on your site without leaving, going back to the search engine, and more than likely not returning to your site.
It's not difficult to use the .htaccess file to redirect users to a custom error page--but to do it you'll need to know the proper error code. The most common ones you'll use are:
  • 400 - Bad request
  • 401 - Authorization Required
  • 403 - Forbidden
  • 404 - File Not Found
  • 500 - Internal Server Error
To use .htaccess with these codes, first you'll need to open up your favorite text editor, create a new document, and in that document, specify the error message that you'll be redirecting like this:
ErrorDocument 404 /filenotfound.html
If you wanted to redirect users for another error, such as 500, Internal Server Error, you would do it like this (and so on):
ErrorDocument 500 /servererror.html
Then you'd just save the .htaccess file (remembering to check that it is saved just like that, without some additional extension), and upload it to your web host's root directory (or whatever directory you are wanting to use it in). 
WWW / Non-WWW  HTAccess

WWW / Non-WWW HTAccess





Force the www.


RewriteEngine On RewriteCond %{HTTP_HOST} ^your-site.com [NC] RewriteRule ^(.*)$ http://www.your-site.com/$1 [L,R=301]



Remove the www.


RewriteEngine On RewriteCond %{HTTP_HOST} !^your-site.com$ [NC] RewriteRule ^(.*)$ http://your-site.com/$1 [L,R=301]
Active Gzip Compression .htaccess

Active Gzip Compression .htaccess



Compression reduces response times by reducing the size of the HTTP response. Gzip is the most popular and effective compression method currently available and generally reduces the response size by about 80%.

# BEGIN GZIP<ifmodule mod_deflate.c>AddOutputFilterByType DEFLATE text/text text/html text/plain text/xml text/css application/x-javascript application/javascript</ifmodule># END GZIP
That's essentially a list of MIME types that gzipping will then apply to. Feel free alter the list to all text based assets you serve.


The HTML5 Boilerplate project offers server configs for all the popular servers. This is it's version for .htaccess

<IfModule mod_filter.c>
    AddOutputFilterByType DEFLATE "application/atom+xml" \
                                  "application/javascript" \
                                  "application/json" \
                                  "application/ld+json" \
                                  "application/manifest+json" \
                                  "application/rdf+xml" \
                                  "application/rss+xml" \
                                  "application/schema+json" \
                                  "application/vnd.geo+json" \
                                  "application/vnd.ms-fontobject" \
                                  "application/x-font-ttf" \
                                  "application/x-javascript" \
                                  "application/x-web-app-manifest+json" \
                                  "application/xhtml+xml" \
                                  "application/xml" \
                                  "font/eot" \
                                  "font/opentype" \
                                  "image/bmp" \
                                  "image/svg+xml" \
                                  "image/vnd.microsoft.icon" \
                                  "image/x-icon" \
                                  "text/cache-manifest" \
                                  "text/css" \
                                  "text/html" \
                                  "text/javascript" \
                                  "text/plain" \
                                  "text/vcard" \
                                  "text/vnd.rim.location.xloc" \
                                  "text/vtt" \
                                  "text/x-component" \
                                  "text/x-cross-domain-policy" \
                                  "text/xml"

</IfModule>




301 Redirect htaccess

301 Redirect htaccess




Redirect a single page


Redirect 301 /oldpage.html http://www.yoursite.com/newpage.html Redirect 301 /oldpage2.html http://www.yoursite.com/folder/


Redirect an entire site


This way does it with links intact. That is www.oldsite.com/sub/view/normal.html will become www.newsite.com/some/view/normal.html. This is extremely helpful when you are just "moving" a site to a new domain. Place this on the OLD site:

Redirect 301 / http://newsite.com/