What's wrong with this rewrite rule? - .htaccess

I have this .htaccess:
RewriteEngine On
RewriteRule ^!/(.*)$ path/to/a/file/$1 [L]
RewriteRule ^(.*)$ path/to/another/file/$1 [L]
I want urls in the form of www.website.com/!/this/ to be rewritten to path/to/a/file. Any URL that doesn't match that pattern should be rewritten to path/to/another/file/.
Here's what I've tried to far:
RewriteEngine On
RewriteRule ^!/(.*)$ path/to/a/file/$1 [L]
RewriteCond ...
RewriteRule ^(.*)$ path/to/another/file/$1 [L]
When using the above rewrite rule, I get a 500 - Internal Error.
How can I fix it?

Try this:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} ^/!
RewriteRule ^!/([a-z0-9_\-\.]+) user/public/$1 [L]
RewriteCond %{REQUEST_URI} !^/!
RewriteRule ^([a-z0-9_\-\.]+)/([a-z0-9_\-\.]+)/?$ $1/controller/front.php/$2 [L]
RewriteCond %{REQUEST_URI} !^/!
RewriteRule ^([a-z0-9_\-]+)/([a-z0-9_\-]+)/([a-z0-9_\-]+)/?$ $1/controller/front.php/$2/$3 [L]

Related

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

.htaccess redirect all index.html to parent folder

I need to redirect all index.html like this
Original URL
www.example.com/lp/index.html
www.example.com/sytem/index.html
Desired URL
www.example.com/lp
www.example.com/sytem
I used the follwing, it redirects successfully but 404 page
RewriteEngine On
RewriteRule ^index\.html$ [R=301,L]
RewriteRule ^(.*)/index\.html$ /$1/ [R=301,L]
You almost got it right, what you did looks like a copy paste error to me. Your first RewriteRule is missing a parameter - there should be a slash before [R=301,L]
Your htaccess should look like this:
RewriteEngine On
RewriteRule ^index\.html$ / [R=301,L]
RewriteRule ^(.*)/index\.html$ /$1/ [R=301,L]
Reference:
http://dense13.com/blog/2012/10/29/removing-index-html-with-mod_rewrite-in-htaccess/
Place this code in your DOCUMENT_ROOT/.htaccess file:
DirectoryIndex index.html
RewriteEngine On
RewriteCond %{THE_REQUEST} /index\.html [NC]
RewriteRule ^(.*?)index\.html$ /$1 [L,R=301,NC,NE]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1/index\.html -f [NC]
RewriteRule ^(.+?)/?$ /$1/index.html [L]
Try putting this in your .htaccess
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ $1/index\.html [PT,L]
PT and L are rewrite flags, for more details about them check http://httpd.apache.org/docs/2.2/rewrite/flags.html
why not redirect index.(anything) ? like index.php? index.xhtml?
RewriteEngine On
RewriteRule ^index\.(.*)$ / [R=301,L]
RewriteRule ^(.*)/index\.(.*)$ /$1/ [R=301,L]
or to base it from the initial URL request only (to avoid conflicts with other rewrite rules):
RewriteEngine On
RewriteCond %{THE_REQUEST} /index\.(.*) [NC]
RewriteRule ^(.*?)index\.(.*)$ /$1 [R=301,L]

.htaccess RewiteCond (QUERY_STRING) with 2 variable and rewrite properly

I need to rewrite my url from:
http://***.com/index.php?cat=VAR&page=1
to:
http://***.com/VAR/1
With 301 redirection.
I got this so far:
RewriteCond %{THE_REQUEST} ^(GET|POST)\ /index.php\?
RewriteCond %{QUERY_STRING} ^cat=(.*)\&page=(.*)
RewriteRule . /%1/%2 [R=301]
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/(files|admin)/
RewriteRule ^(.*)/(.*)$ /index.php?cat=$1&page=$2 [L]
But the first 3 rules doesn't seems to work at all. (I'm a beginner in htaccess)
How can i fix this problem? Thanks!
EDIT :
Thanks to Jassie, the solution:
RewriteCond %{THE_REQUEST} ^(GET|POST)\ /index.php\?
RewriteCond %{QUERY_STRING} ^cat=(.*)\&page=(.*)
RewriteRule ^(.*)/(.*)$ /index.php?cat=$1&page=$2 [L,QSA]
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/(files|admin)/
RewriteRule ^(.*)/(.*)$ /index.php?cat=$1&page=$2 [L]
change it to RewriteRule ^(.)/(.)$ /index.php?cat=$1&page=$2 [L,QSA] and try

Htaccess mod rewrite help

I want to redirect the url:
http://www.mysite.com/invite/YhMck/en
to
http://www.mysite.com/auth/accept_invite/YhMck/en
can any please help me with the RewiteRule
RewriteCond %{HTTP_HOST} ^mysite.com/invite/YhMck/en
RewriteRule (.*) http://www.mysite.com/auth/accept_invite/YhMck/en/$1 [R=301,L]
RewriteEngine On
add this to your .htaccess in your documentroot
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_URI} ^/invite
RewriteRule ^invite/([^/]+)/(\w{2})$ auth/accept_invite/$1/$2
from comments:
These rules:
RewriteCond $1 !^(index\.php|robots\.txt|images|img|css|js|fonts)
RewriteRule ^(.*)$ /index.php/$1 [L]
Will add index.php to every rule that does not begin with index.php|robots\.txt|images|img|css|js|fonts
So your /invite would also have been re-written to /index.php/invite...
Try this in the same order
RewriteEngine on
RewriteBase /
RewriteCond %{ENV:REDIRECT_STATUS} 200
RewriteRule ^ - [L]
RewriteCond %{REQUEST_URI} ^/invite
RewriteRule ^invite/([^/]+)/(\w{2})$ auth/accept_invite/$1/$2 [L]
RewriteCond $1 !^(auth/|index\.php|robots\.txt|images|img|css|js|fonts)
RewriteRule ^(.*)$ /index.php/$1 [L]

Redirect 301 & RewriteCond Collision

I have in my .htaccess file the following code:
Options +FollowSymLinks
RewriteEngine On
Redirect 301 /index.asp http://www.website.com/index.php
Redirect 301 /contact.asp http://www.website.com/contact.php
RewriteCond %{REQUEST_METHOD} !POST
RewriteRule ^(.*/)?index\.([a-zA-Z0-9]{3,4})$ /$1 [R=301,L]
ErrorDocument 404 error.php
RewriteRule ^sitemap.xml sitemap.php
RewriteCond %{ENV:REDIRECT_STATUS} 200
RewriteRule .* - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} ^(.*)$
RewriteRule (.*) seofile.php?word=%{REQUEST_URI}
Now,
for some reason, when I entry to contact.asp I'm getting
http://www.website.com/seofile.php?word=/contact.asp
how can I fix it?
Try using rewriterule instead of Redirect (I think rewrite rules are applied before redirect rules). So something like:
RewriteRule ^index.asp / [R=301,L]
RewriteRule ^contact.asp /contact.php [R=301,L]
PS. the first rule can be removed, as you already rewrite every index.xxx file.
Advice: try to separate things that don't belong to RewriteRules.
Try this code and please tell me if it works:
ErrorDocument 404 error.php
Options +FollowSymLinks
RewriteEngine On
RewriteRule ^(index|contact)\.asp $1.php [QSA,R=301,L]
RewriteRule ^sitemap\.xml$ sitemap.php [L]
RewriteCond %{REQUEST_METHOD} !POST
RewriteRule ^(.*/)?index\.([a-zA-Z0-9]{3,4})$ /$1 [R=301,L]
RewriteCond %{ENV:REDIRECT_STATUS} 200
RewriteRule (.*) - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*) seofile.php?word=%{REQUEST_URI}

Resources