How can I convert some rewrite rules in .htaccess?
My old site had the following:
RewriteRule ^([^/]*)\.html$ index.php?name=$1 [L]
which would rewrite url to : domain.com/some-page.html
Now I'm using different rewrite with virtual directories like:
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?url=$1 [QSA,L]
this translates into domain.com/page/somepage/
I have tried using
Redirect 301 some-page.html page/somepage/
but keep getting 404 error
You can use this :
RewriteEngine on
RewriteCond %{THE_REQUEST} /([^.]+)\.html [NC]
RewriteRule ^ /page/$1 [L,R=301]
Related
I have a .htaccess file inside folder public (root/public)
I would like to achieve the following transformations:
/index.php -> /
index.php?site=siteName -> /siteName/
/siteName/ or /siteName -> serves as ?site=siteName
Here is my complete .htaccess file so far (with case 3 solved):
Allow from all
RewriteEngine On
RewriteRule ^([a-zA-Z0-9]+)$ index.php?current=$1
RewriteRule ^([a-zA-Z0-9]+)/$ index.php?current=$1
You can use an additional rules for redirection of old URL to pretty URL and index.php removal:
RewriteEngine On
# external redirect from actual URL to pretty one
RewriteCond %{THE_REQUEST} /index\.php\?current=([^\s&]+) [NC]
RewriteRule ^ /%1? [R=302,L,NE]
# remove index.php
RewriteCond %{THE_REQUEST} /index\.php [NC]
RewriteRule ^(.*)index\.php$ /$1 [L,R=302,NC,NE]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([a-zA-Z0-9]+)/?$ index.php?current=$1 [L,QSA]
I have a page at example.com/news.php. This page is already indexed by Google. I would like to ditch this page and instead use example.com/news.
So essentially I need to report 301 on news.php and also redirect any request to /news to news.php.
When I am using the below in .htaccess I am getting a redirect loop:
Redirect 301 /news.php http://example.com/news
RewriteRule ^news$ news.php [L]
How would I go about doing the desired?
Try :
RewriteEngine on
RewriteCond %{THE_REQUEST} /news\.php [NC]
RewriteRule ^ /news [L,R=301]
RewriteRule ^news/?$ news.php [L]
Edit as per your comment :
To redirect any php file , you can use :
RewriteEngine on
RewriteCond %{THE_REQUEST} /([^.]+)\.php [NC]
RewriteRule ^ /%1 [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+)/?$ $1.php [L]
The past couple of hours I am trying to create custom redirects from an old mambo website to new drupal 7 website with the .htaccess file that exists in my drupal's root.
What I want to do is...
301 Redirect
http://mysite.com/index.php?option=com_content&task=blogsection&id=11&Itemid=54
to
http://mysite.com/this-is-the-new-page
This is my .htaccess file...
RewriteEngine on
RewriteRule ^index.php?option=com_content&task=blogsection&id=11&Itemid=54$ http://mysite.com/this-is-the-new-page [R=301,L]
RewriteRule "(^|/)\." - [F]
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^ http://%1%{REQUEST_URI} [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !=/favicon.ico
RewriteRule ^ index.php [L]
I am sure that it has something to do with this line...
RewriteRule ^ index.php [L]
But I don't get it!
You see if I use this...
RewriteRule ^option=com_content&task=blogsection&id=11&Itemid=54$ http://mysite.com/this-is-the-new-page [R=301,L]
instead of this...
RewriteRule ^index.php?option=com_content&task=blogsection&id=11&Itemid=54$ http://mysite.com/this-is-the-new-page [R=301,L]
and test it with firefox and LiveHTTP Headers addon it works!
Any suggestions?!
Thanks!
The query string is not part of the URL path pattern. If you want to base a rule on the query string, you must do so in a RewriteCond
RewriteEngine on
RewriteCond %{QUERY_STRING} option=com_content&task=blogsection&id=11&Itemid=54
RewriteRule ^index.php$ /this-is-the-new-page? [R,L]
I am stuck trying to write a rewrite rule for my htaccess file which should forward from for example www.example.com/en to www.example.com/en/
I tried: RewriteRule ^en/?$ en [L]
but nothing happens. Does someone have an idea how to accomplish this?
You can use following rules in your .htaccess file:
Options +FollowSymLinks -MultiViews
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(?![^/]*/).*$ %{REQUEST_URI}/ [L,R]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]*)/$ index.php?lang=$1 [QSA,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]*)/([^/]*)/?$ index.php?action=$2&lang=$1 [QSA,L]
This will:
externally redirect: /en to /en/ and internally redirect this to /index.php?lang=en
internall redirect: /fr/login to /index.php?action=login&lang=fr
Additionally: As per your comment you will need this rule to add www in domain:
RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
Use RedirectMatch:
RedirectMatch Permanent ^/en$ /en/
My .htaccess file is:
Redirect 301 http://domain.com/news/articles?dtMain_start=150 http://domain.com/news/articles
Redirect 301 http://domain.com/news/articles?dtMain_start=160 http://domain.com/news/articles
Redirect 301 http://domain.com/news/articles?dtMain_start=170 http://domain.com/news/articles
#
RewriteEngine On
RewriteBase /
# The Friendly URLs part
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]
I also have to incorporate the following rule
RewriteEngine On
RewriteCond %{HTTP_HOST} ^domain.com
RewriteRule (.*) http://www.domain.co.uk/$1 [R=301,L]
I cannot get them to work together... can anyone help...
I tried just stacking the Redirects before the RewriteCond and I get this...
http://www.domain.com/news/articles?q=news/articles?dbMain_start=150
ie http://domain.com/newpage?q=oldpage
Okay Mod_Alias and Mod_Rewrite don't like each other.
Can I write something like:
RewriteCond %{REQUEST_QUERY_STRING} ^.*&bodgeredirect=true$
RewriteRule ^(.*)&bodgeredirect=true$ index.php?q=$1 [L,QSA]
First of all: There is not mod_redirect. Redirect is a directive of mod_alias.
And the Redirect directive, like any other directive of mod_alias, does only work with the URL path. So your Redirect directives won’t work as expected. Use mod_rewrite equivalents instead:
RewriteCond %{HTTP_HOST} =example.com
RewriteRule (.*) http://www.example.com/$1 [R=301,L]
RewriteCond %{QUERY_STRING} ^dtMain_start=(150|160|170)$
RewriteRule ^news/articles$ /news/articles? [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]
In general it is not a good idea to mix mod_alias and mod_rewrite if the patterns coincide with each other.