mod_rewrite config for / - .htaccess

I have the following .htaccess config
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^example.com [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]
RewriteRule \.git - [F,L]
RewriteRule ^help help.php [L]
RewriteRule ^home home.php [L]
RewriteRule ^profile profile.php [L]
RewriteRule ^index index.php [L]
RewriteRule ^users/([0-9]+) profile.php?id=$1 [L]
RewriteRule ^([A-Za-z0-9-]+)?$ profile.php?u=$1 [L]
Now, whenever somebody visits the landing page, they get redirected using the last rule for profile.php?u=$1.
How do I change the configuration so that www.example.com and www.example.com/ are mapped to index.php and not profile.php?

Match the empty string or single slash just after the ^index rule:
RewriteRule ^help help.php [L]
RewriteRule ^home home.php [L]
RewriteRule ^profile profile.php [L]
RewriteRule ^index index.php [L]
# Match root request with optional slash
RewriteRule ^/?$ index.php [L]

I will suggest not to do it this way.
Instead, simply user this
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>
This will send all your requests to index.php page, from there create a router.php and pass on the requests to that page, using php.
but in case you do, just add
RewriteRule ^/?$ index.php [L]
Like Michael suggested.
Here is a simple tool to test your rules, if you wish to
Apache RewriteRule tester

Related

How to remove index.php in Magento 1.9 using .htaccess

I have such URL at my Magento website as http://magento.store/about and http://magento.store/index.php/about. I don't need these two pages, I need them to be properly rewrited in .htaccess file. I've tried everything which I could find at StackOverflow but nothing helps. I configured URL-rewrites in configuration using adpanel, used different solutions but the most, I got is to do these redirects but my adpanel doesn't writes any changes to the website's database (I used this solution:
RedirectMatch 301 ^/index.php/((?!admin).*) http://www.magento.store/$1
In this case it is impossible to change anything in adpanel. The usual solutions such as
RewriteEngine on
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
do not also work. They are ignored as far as I understand.
How can I solve this issue?
Here is my .htaccess file (the part about the mod_rewrite):
<IfModule mod_rewrite.c>
Options +FollowSymLinks
RewriteEngine on
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
RewriteCond %{HTTP_HOST} ^magento\.store$ [NC]
RewriteRule ^(.*)$ http://www.magento.store/$1 [L,R=301]
# redirect pagination
RewriteCond %{QUERY_STRING} ^p=1$
RewriteRule ^(.+)$ http://www.magento.store/$1? [R=301,L]
RewriteRule ^pagename$ http://www.magento.store/page-name [L,R=301]
#RewriteCond %{REQUEST_URI} !/admin/
#RewriteRule ^index\.php/(.+)$ /$1 [R,L]
#RewriteRule ^index\.php/?$ / [R,L]
#RedirectMatch 301 ^/index.php/(.*)$ /$1
#RedirectMatch 301 ^/index.php/((?!admin).*) http://magento.store/$1
RewriteRule ^api/rest api.php?type=rest [QSA,L]
############################################
## workaround for HTTP authorization
## in CGI environment
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
############################################
## TRACE and TRACK HTTP methods disabled to prevent XSS attacks
RewriteCond %{REQUEST_METHOD} ^TRAC[EK]
RewriteRule .* - [L,R=405]
############################################
## always send 404 on missing files in these folders
RewriteCond %{REQUEST_URI} !^/(media|skin|js)/
############################################
## never rewrite for existing files, directories and links
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
############################################
## rewrite everything else to index.php
RewriteRule .* index.php [L]
</IfModule>
Is it possible that I have some conflicts inf my file?
You can capture the part after index.php and redirect the client with
RewriteEngine on
RewriteRule ^index\.php/(.+)$ /$1 [R,L]
RewriteRule ^index\.php/?$ / [R,L]
This redirects all requests starting with index.php/ to the URL without index.php. Additionally index.php and index.php/ are redirected to the home page.
Use below .htaccess rule:
## index.php on default domain
RewriteCond %{THE_REQUEST} ^.*/index.php
RewriteCond %{REQUEST_URI} !^/downloader.*$
RewriteRule ^(.*)index.php$ http://www.idesignmydrapes.com [R=301,L]
RewriteCond %{HTTP_HOST} ^idesignmydrapes\.com$ [NC]
Thank you, everybody, for your help. Finally, I made the solution and it is the following:
Options +FollowSymLinks
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_URI} !/adpanel/
RewriteRule ^index\.php/(.*)$ http://magento.store/$1 [L,R=301]

URL rewrite for part of domain name

I feel this should be an easy fix but I'm struggling to get it done right.
I have the URL:
http://www.testing.com/toursgbr/my-post
http://www.testing.com/toursgbr/my-post-2
I need to rewrite the URL to:
http://www.testing.com/tours/gbr/my-post
http://www.testing.com/tours/gbr/my-post-2
I got as far as the following:
RewriteRule ^toursgbr/(.*) /tours\/gbr/$1 [L]
This is what's currently in the htaccess file:
RewriteEngine on
ErrorDocument 404 http://www.ausweb.com.au/web-hosting
AddHandler server-parsed html
# 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
RewriteCond %{HTTP_HOST} ^seabreezepark\.com\.au$ [OR]
RewriteCond %{HTTP_HOST} ^www\.seabreezepark\.com\.au$
RewriteRule ^/?$ "http\:\/\/theseabreezepark\.com\.au\/" [R=301,L]
RewriteRule ^toursgbr/(.+)$ /tours/gbr/$1 [NC,L]
and got nowhere pretty fast. I just want to look for the word "toursgbr" and change it to "tours/gbr" in summary.
Put the Following code at root .htaccess file :
RewriteEngine on
RewriteBase /
RewriteCond %{THE_REQUEST} !\s/+tours/gbr/ [NC]
# the above line will exclude any request having tours/gbr/ from the follwoing rule.
RewriteRule ^toursgbr/(.*)$ tours/gbr/$1 [R=302,L,NE]
# the above line will change any requested url having toursgbr/ to be tours/gbr/ temporary
# and you can change it to permanent by changing [R=302,L,NE] to [R=301,L,NE]
# but check the code as it is first then change it
RewriteRule ^tours/gbr/(.*)$ toursgbr/$1 [L,NC]
# the above line will internally map any request having tours/gbr/ to its original path
Try :
RewriteRule ^toursgbr/(.+)$ /tours/gbr/$1 [NC,L]
Do not use the full url in rewrite target if you want to internally redirect /toursgbr/foo to /tours/gbr/foo without changing the url in browser.
Your corrected htaccess :
ErrorDocument 404 http://www.ausweb.com.au/web-hosting
AddHandler server-parsed html
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} ^(www\.)?seabreezepark\.com\.au$
RewriteRule ^/?$ http://theseabreezepark.com.au/ [L,R=301]
RewriteRule ^toursgbr/(.+)$ /tours/gbr/$1 [NC,L]
# BEGIN WordPress
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]

URL Re Writing issue double http issue

I am having a issue with my urls google have crawled double http for example
http://example.com/example.com/c-title-id.html
and all the interlinked pages like that
please can any one help me out to redirect all the urls with 301 with .htacess
My htacess code is
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.website\.com [NC]
RewriteRule ^(.*)$ http://website.com/$1 [L,R=301]
RewriteRule category-([0-9]+)-(.*)-([0-9]+)\.html$ category.php?cid=$1&name=$2&page=$3.php [L]
RewriteRule search-(.*)-([0-9]+)\.html$ search.php?term=$1&page=$2.php [L]
RewriteRule all-covers-([0-9]+)\.html$ all-covers.php?page=$1.php [L]
RewriteRule index-([0-9]+)\.html$ index.php?page=$1.php [L]
RewriteRule featured-covers-([0-9]+)\.html$ featured-covers.php?page=$1.php [L]
RewriteRule top-downloaded-([0-9]+)\.html$ top-downloaded.php?page=$1.php [L]
RewriteRule c-([0-9]+)(.*)\.html$ cover.php?id=$1&name=$2.php [L]
RewriteRule sitemap.xml$ sitemap.php [L]
RewriteRule sitemap1.xml$ sitemap1.php [L]
RewriteRule rss.xml$ rss.php [L]
RewriteRule ^(.*)\.html$ $1.php [L]
ErrorDocument 404 /404.html
I want to redirect all of them automaticaly.
Should be as simple as having this at the top of your rules:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.website\.com [NC]
RewriteRule ^(.*)$ http://website.com/$1 [L,R=301]
RewriteRule example\.com/(.*)$ /$1 [R=301,L]
#... rest of your rules...

How to hide page name and extention from url using .htaccess

I want to hide all my page names and extension from url,
htt://www.domain.com/innerpage.php
to
http://www.domain.com/
and
http://www.domain.com/subfolder/innerpage.php
to
http://www.domain.com/subfolder/
and
http://www.domain.com/subfolder/subfolder/innerpage.php
to
http://www.domain.com/subfolder/subfolder/
I used like
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !\..+$
RewriteCond %{REQUEST_URI} !/$
RewriteRule ^(.*)$ http://www.domain.com/$1/ [R=301,L]
RewriteRule ^(.*)$ http://www.domain.com/subfolder/$1/ [R=301,L]
RewriteRule ^(.*)$ http://www.domain.com/subfolder/subfolder/$1/ [R=301,L]
RewriteRule ^(.*)/$ $1.php [L]
its not work
I think this will work for you
DirectoryIndex innerpage.php index.php index.html index.htm
Put this as first line of your .htaccess. This directive will look for innerpage.php if no page has been specified for a directory. If you want to hide all pages in your site URL's then it's not a good idea IMO.
Give this set of directives a try:
Options +FollowSymlinks
RewriteEngine on
RewriteRule ^/?$ /innerpage.php
RewriteRule ^([a-z0-9]+)/?$ /$1/innerpage.php
RewriteRule ^([a-z0-9]+)/([a-z0-9]+)/?$ /$1/$2/innerpage.php
RewriteRule ^innerpage.php$ / [R]
RewriteRule ^([a-z0-9]+)/innerpage.php$ /$1 [R]
RewriteRule ^([a-z0-9]+)/([a-z0-9]+)/innerpage.php$ /$1/$2 [R]
And be surprise what will happened...

Delete ugly ?pg=1 from URL via .htacess RewriteRule

I have pagination plugin in my CMS which produces ?pg=1 links. I want to redirect this url without this ugly postfix because without it CMS shows first page too.
So, i have url like http://site.ru/category/schock-crane/?pg=1 or http://site.ru/moyka/?type=granit&pg=1
I want to redirect such urls to http://site.ru/category/schock-crane/ and http://site.ru/moyka/?type=granit respectly.
I tried this .htaccess code
RewriteRule (.*)(&|\?)pg=1$ $1 [R=301,L]
I tried this regexp code at regexp trainers - it worked. But at live server no redirect happens.
Here is whole .htaccess file:
AddDefaultCharset Off
#DirectoryIndex index.php index.html
Options +FollowSymLinks
Options -Indexes
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
# fix /?pg=1
RewriteRule (.*)(&|\?)pg=1$ $1 [R=301,L]
# fix .palitra-tab
RewriteCond %{REQUEST_URI} -tab$
RewriteRule (.*)/([0-9]*)/\.(.*)-tab?$ http://site.ru/redirect.php?id=$2 [R=301,L]
RewriteCond %{REQUEST_URI} ^/csss/.* [NC]
RewriteRule csss/(.*)\.css$ css.php?n=$1 [L]
#RewriteRule ^(.*)/csss/(.*) /test.php [L]
RewriteRule ^(.*)/base([0-9]+)/?$ $1?base=$2
#RewriteCond %{REQUEST_FILENAME} -f
#RewriteRule \.(gif|jpeg|jpg|png)$ /images/watermark/index.php [L]
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^(.+) - [PT,L]
RewriteCond %{REQUEST_URI} !=/favicon.ico
RewriteRule ^(.*) index.php
RewriteCond %{HTTP:Authorization} !^$
RewriteRule .* - [E=REMOTE_USER:%{HTTP:Authorization}]
</IfModule>
You need to use RewriteCond to examine the query string:
RewriteCond %{QUERY_STRING} ^(([^&]*&)*)pg=1(&(.*)|$)
RewriteCond %1%4 (.*?)&?$
RewriteRule ^ %{REQUEST_URI}?%1 [R=301,L]
The second RewriteCond is just to remove a trailing &.
Maybe the missing ^ or / kills the redirect
# fix /?pg=1
RewriteRule ^(.*)(&|\?)pg=1$ /$1 [R=301,L]
or try
RewriteRule ^(.*)(&|\?)pg=1$ http://%{HTTP_HOST}/$1 [R=301,L]

Resources