.htaccess not letting robot.txt through - .htaccess

I have the following .htaccess file in my root:
Options +FollowSymlinks
RewriteEngine on
RewriteBase /
RewriteRule ^(.*)\/(\?.*)?$ $1$2 [R=301,L]
RewriteRule ^([^/]*)$ index.php?page=$1 [NC]
This works as it should for shortening all my URLs to website.com/something
The problem is Google can't find my robots.txt file in my root. The above file isn't letting it through. when It type website.com/robots.txt I get a 404 not found. But if I comment out the above .htaccess code I can get to it just fine.
How can I edit my .htaccess file to let robots.txt through without interfering with my other URLs?

RewriteEngine on
RewriteRule ^robots.txt - [L]
Second line will exclude robots.txt from URL rewritting rules .
Try above code

I tried both suggestions and they both work great. However I went with Kiran's answer simply because it's a shorter syntax. This is what I ended up with.
Options +FollowSymlinks
RewriteEngine on
RewriteBase /
# remove trailing slash
RewriteRule ^(.*)\/(\?.*)?$ $1$2 [R=301,L]
# Allow Robots.txt to pass through
RewriteRule ^robots.txt - [L]
RewriteRule ^([^/]*)$ index.php?page=$1 [NC]

You can use this solution in your .htaccess file:
Options +FollowSymlinks
RewriteEngine on
RewriteBase /
RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteRule ^(.*)$ index.php?page=$1 [L]
This rewrites all your requests to index.php?page=, except the files specified in the RewriteCond list.

Find the line that already exists in your .htaccess that says this:
RewriteRule ^sitemap.xml$ index.php?route=feed/google_sitemap [L]
And change it to this:
RewriteRule ^itemap.xml$ index.php?route=feed/google_sitemap [L]

Related

How do I do simple flat link to .php file redirect?

My current .htaccess file is as
Options +FollowSymlinks
RewriteEngine On
RewriteRule ^(.+)\.html$ $1.php [L]
This is at the directory level, I do not want to have a site-wide .htaccess.
I can redirect ../file.html to ../file.php but I am not able to do it for ../file -> ../file.php
My URL would be www.domain.com/sub1/sub2/file/ The file that should be run is www.domain.com/sub1/sub2/file.php But I want this rule to work only under sub2. So the rule will be included in the .htaccess at sub2
What exactly I need to change on this code to be able to do that?
More specifically, why is this not working?
Options +FollowSymlinks
RewriteEngine On
RewriteRule ^(.*)$ $1.php [L]
This is working as I want it to work:
Options +FollowSymlinks
RewriteEngine On
RewriteRule ^([a-zA-Z0-9_-]+)/?$ $1.php [L]
You can change your existing rule to this to work under /sub1/sub2/.htaccess
Options +FollowSymlinks
RewriteEngine On
RewriteBase /sub1/sub2/
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^./]+)/?$ $1.php [L]

htaccess redirection and rewrite from php to html

I am stuck with .htaccess modification, need a little help.
First off, here is whats inside my htaccess.
RewriteEngine on
RewriteRule ^(.*)\.html$ $1.php [nc]
For example, I created the file named test.php and uploaded to my server.
I want my server to behave like this.
http://example.com/test/test.html -> http://example.com/test/test.html(as it is)
http://example.com/test/test.php -> http://example.com/test/test.html
but with the .htaccess I have right now,
I still have both .php and .html which may be considered as file duplication by search engine crawler like Google robot (isn't it?).
Any help appreciated.
try this .htaccess code
# Enable Rewrite Engine
RewriteEngine on
#Create friendly URL
RewriteRule ^test/(.*)\.html$ test/$1\.php [L]
OR
RewriteCond %{REQUEST_URI} ^(.*)\.php$
RewriteRule ^(.*) /$1.html [L]
OR
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
## .php to .html
# To externally redirect /test/test.php to /test/test.html
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1.html [R,L,NC]
This is a configuration file of apache '.htaccess' if you still want to configure then change
RewriteEngine on
I hope this work
You may try this in one .htaccess file in root directory:
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} !\.html [NC]
RewriteRule ^([^/]+)/([^.]+)\.php /$1/$2.html [R=301,NC,L]
For silent mapping, replace [R=301,NC,L] with [NC,L]

.htaccess mod_rewrite RewriteRule for categories

I wanted to have http://www.mywebsite.com/cd2012/legal rather than http://www.mywebsite.com/cd2012/index.php?legal=1
i have tried
Options +FollowSymLinks
RewriteEngine on
RewriteRule guarantees/(.*) cd2012/index\.php/legal=$1 [R=301,L]
RewriteRule guarantees cd2012/index\.php/legal=$1 [R=301,L]
RewriteRule ^guarantees/$ cd2012/index\.php/legal=$1 [R=301,L]
none of them working.
If you want to rewrite, you don't want to use [R=301,L] as this basically means "tell the users browser that this document is permanently moved to this location"
You also shouldn't escape the final path, as it's not regex.
Instead, do this (according to your example):
RewriteEngine on
RewriteRule ^cd2012/?$ cd2012/index.php
RewriteRule ^cd2012/(.*) cd2012/index.php?$1=1
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^([a-zA-Z0-9]+)/$ index.php?$1=1 [L]
RewriteRule ^([a-zA-Z0-9]+)$ index.php?$1=1 [L]
</IfModule>
Right now this is what does the magic for me.
Try this :
RewriteEngine on
RewriteBase /
RewriteRule ^cd2012/legal$ cd2012/index.php?legal=1 [L]

godaddy - htaccess condition not working

I have a mod rewrite working on localhost, but not in godaddy shared host.
Options -Multiviews
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{DOCUMENT_ROOT}/WebShop/View/public%{REQUEST_URI} -f
RewriteRule ^(.*)$ WebShop/View/public/$1 [L]
RewriteRule ^(.*)$ entryPoint.php [QSA,L]
The problem is with the RewriteCond part. It cannot find the exsiting files in the subfolder. I tried with absolute path, relative path, rewritebase everything, but neither of them worked. Godaddy doesn't grant access to the rewritelog... Any idea how to solve this problem?
Edit:
The htaccess is in a subfolder, and in the document root is a htaccess which splits the request by domain into subfolders:
Options -Multiviews
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ([^\.]+\.[^\.]+)$
RewriteRule ^(.*)$ %1/$1 [L,QSA]
And the real problem is, that the %{REQUEST_URI} in the htaccess of the subfolder contains the name of the subfolder too :S
Edit2:
Ok I have a partial solution:
Options -Multiviews
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} ^/([^/]+)/(.*)$
RewriteCond %{DOCUMENT_ROOT}/%1/WebShop/View/public/%2 -f
RewriteRule ^(.*)$ entryPoint.php?file=%2 [L]
RewriteRule ^(.*)$ entryPoint.php [QSA,L]
This prints the file name if exists, but it's kind of joke, cause if I change the second rule to
RewriteRule ^(.*)$ x.php [QSA,L]
Then everything goes to the x.php. I'm wondering why mod rewrite never recognize the L flag?
(go drink a beer)
It's maybe because of some sort of strange directory settings, but if you redirect to a subDirectory on a godaddy server, you have to create there an empty .htaccess file with RewriteEngine On, otherwise it won't work.
The preceding code in the edit2 section works well, but not on a godaddy server... Here is the file access version of it:
Options -Multiviews
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} ^/([^/]+)/(.*)$
RewriteCond %{DOCUMENT_ROOT}/%1/WebShop/View/public/%2 -f
RewriteRule ^(.*)$ WebShop/View/public/%2 [L]
RewriteRule ^(.*)$ entryPoint.php [QSA,L]
And I placed an empty .htaccess file with RewriteEngine On in the "WebShop/View/public" directory, that fixed the L flag problem.

htaccess rewrite url remove subdirectory

I'm trying to rewrite
http://www.example.com/directory/folder/*
to
http://www.example.com/directory/*
the htaccess file is in directory
this is my .htaccess file :
RewriteEngine on
RewriteBase /directory/
RewriteRule ^(.*)$ folder/$1 [L]
Any help would be much appreciated.
This is what I ended up doing :
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{REQUEST_URI} !(.*)folder
RewriteRule ^(.*)$ folder/$1 [L]
What about this?
RewriteEngine on
RewriteRule ^folder/(.*) /directory/$1 [L]
Or you can go without [L] or use [R] instead.

Resources