I have a url, lets say its blog.art.ca/Customer/AAAABBBB/index.html
and I want to hide Customer and AAAABBBB. Now AAAABBBB can be any 8 character alphanumeric code.
Options +FollowSymLinks -Multiviews -Indexes
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^Customer/(.*)$ /$1 [L,R]
</IfModule>
I've tried numerous things, however, I either get ERR_TOO_MANY_REDIRECTS or it just causes the server to crash. Any suggestions would be much appreciated!
You still need to allow the requests to reach the actual requested resources, so you'll need two sets of rules, one to the browser, to show the new shorter URL (301 permanent redirect), and a second set to undo this mapping back to the original URL, so that Apache can find the right stuff to serve eg.
RewriteBase /
# Remove Customer/AAAABBB from the URL shown in the browser.
RewriteCond %{QUERY_STRING} !customer=
RewriteRule ^Customer/([^/]+)/(.*) /$2?customer=$1 [L,QSA,R=301]
# Internally undo any masked rewrites.
RewriteCond %{QUERY_STRING} customer=([^&]+)
RewriteRule (.*) /Customer/%1/$1 [L]
Related
When I try to redirect my page, nothing happens. If anyone tries to enter forum.example.com/forumdisplay.php?fid=1 I want them to be redirected to forum.example.com/forum-1.php. I am new to .htaccess and couldn't figure it out.
.htaccess file:
Options -MultiViews
RewriteEngine on
Redirect 301 /forumdisplay.php?fid=([0-9]+).php /forum-$1.php
RewriteRule ^forum-([0-9]+)\.php$ forumdisplay.php?fid=$1 [L,QSA]
Any help will be appreciated.
Redirect (mod_alias) and RewriteRule (mod_rewrite) belong to two different modules. You need to use mod_rewrite only for this. Try something like the following:
Options -MultiViews
RewriteEngine on
# Redirect direct requests for the "real" URL
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{QUERY_STRING} ^fid=([0-9]+)
RewriteRule ^forumdisplay\.php$ /forum-%1.php? [R=301,L]
# Internally rewrite back to the "real" URL
RewriteRule ^forum-([0-9]+)\.php$ forumdisplay.php?fid=$1 [L,QSA]
(Redirect doesn't use regex either.)
UPDATE: I've appended a ? onto the end of the first RewriteRule substitution (ie. /forum-%1.php?). By specifying an empty query string, it removes the query string from the request. Alternatively, you can use the QSD flag on Apache 2.4+
On GoDaddy Linux hosting, really would like to get this mod rewrite working, but having a strange problem. Very simple code looks like this:
Options +FollowSymLinks -MultiViews
rewriteengine on
RewriteBase /
## clean URLs
RewriteRule ^blog/([^/.]+)/?$ blog.php?title=$1 [L]
then as a test I went to blog.php?title=testing it did as it was supposed to and rewrote the URL to blog/testing. But then when I went to blog.php?title=test or blog.php?title=new it did not rewrite the URL, so i tried going to blog/test and blog/new and it gave me a 404 error. I thought something broke. So i went back to blog.php?title=testing and everything worked again. So it only worked for the first URL I entered.
Also I have this domain set up in a folder under root. so root is example.com but this site is example.com/something then GoDaddy turns that into something.com.
I want clean URLs, I still want the php to be able to use $_GET on the querystring, however I want the nice seo URL like example.com/blog/title
First rule should redirect your php file to the pretty URL second rule should internally redirect the pretty URL to the old one internally so its not visible to the user:
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /
# Redirect /blog.php?title=anything to /blog/anything
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+blog\.php\?title=([^\s&]+)&? [NC]
RewriteRule ^ /blog/%1? [R=302,L]
# Internally forward /blog/anything to /blog.php?title=anything
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^blog/([\w-]+)/?$ /blog.php?title=$1 [L]
QUICK UPDATE
Ok getting there
Is this mod_alias?
RedirectMatch 301 ^/ http://brightmist.co.uk/
I've added this one line of code underneath everything and it appears to work, however my other directories such as http://brightmist.co.uk/blog/2013/02/23/manchester-art-gallery-feb-2013 are telling google these pages have temporarily moved - see http://www.internetofficer.com/seo-tool/redirect-check/
Does this mean I have to go right the way though my site and add a tone of redirects?
ORIGINAL QUESTION
I have a new website.
I'd like to redirect all of my old site links from http://artygirl.co.uk to my new one http://brightmist.co.uk/
I'm primarily a designer with years of experience using mainly in Photoshop, CSS, HTML, Wordpress, and jQuery but I don't know much about editing things like the htaccess file. And I don't want to get it wrong as it means google ranking drops etc
Does anyone know of any script I can paste into the bottom of my htaccess file, I'd like it to redirect all links/pages on the site to the same place as before. For example if I type http://artygirl.co.uk/buy-art-prints-cheshire/ I want it to go to http://brightmist.co.uk/buy-art-prints-cheshire/ I'm using the same host, they've just re-pointed the domain
Among other things my host has recently added the following code, I assume this is also to do with the domain mapping, also here is my whole htaccess file -
ErrorDocument 401 /forms/401.html
ErrorDocument 403 /forms/403.html
RewriteEngine On
RewriteBase /
#uploaded files
RewriteRule ^(.*/)?files/$ index.php [L]
RewriteCond %{REQUEST_URI} !.*wp-content/plugins.*
RewriteRule ^(.*/)?files/(.*) wp-includes/ms-files.php?file=$2 [L]
# add a trailing slash to /wp-admin
RewriteCond %{REQUEST_URI} ^.*/wp-admin$
RewriteRule ^(.+)$ $1/ [R=301,L]
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule . - [L]
RewriteRule ^([_0-9a-zA-Z-]+/)?(wp-.*) $2 [L]
RewriteRule ^([_0-9a-zA-Z-]+/)?(.*\.php)$ $2 [L]
RewriteRule . index.php [L]
<IfModule mod_security.c>
<Files async-upload.php>
SecFilterEngine Off
SecFilterScanPOST Off
</Files>
</IfModule>
<Files 403.shtml>
order allow,deny
allow from all
</Files>
deny from 218.143.4.127
deny from 143.90.221.204
deny from 95.135.78.190
deny from 114.108.150.74
deny from 95.135.111.205
deny from 91.124.239.150
deny from 94.178.2.93
deny from 91.124.206.118
deny from 91.124.226.116
deny from 118.98.32.34
deny from 94.180.252.133
deny from 58.27.140.58
deny from 77.93.197.83
deny from 88.191.63.27
# Hotlink Protection START #
RewriteEngine on
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^http(s)?://(www\.)?brightmist.co.uk [NC]
RewriteCond %{HTTP_REFERER} !^http(s)?://(www\.)?google.com [NC]
RewriteRule \.(jpg|jpeg|png|gif)$ - [NC,F,L]
# Hotlink Protection END #
I'd like to redirect all of my old site links from http://artygirl.co.uk/ to my new one http://brightmist.co.uk/
There are several ways to achieve that, all of them should be implemented in one .htaccess file in http://artygirl.co.uk/ root directory.
There is no need to check for the incoming domain as it must be artygirl.co.uk, where the .htaccess file is located.
To use any of the following options, copy-paste the corresponding directive or rule-set into one empty .htaccess file in http://artygirl.co.uk root directory.
The fastest one is a simple Redirect using one mod_alias directive:
Redirect 301 / http://brightmist.co.uk/
Any path in the incoming URL will be appended automatically to the redirected URL.
To redirect only certain paths using another mod_alias directive:
RedirectMatch 301 ^/(.*) http://brightmist.co.uk/$1
Although this example redirects everything, the regex ^/(.*) can be modified to match only certain URL-path pattern.
To redirect only certain paths using mod_rewrite directives:
Options +FollowSymlinks -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*) http://brightmist.co.uk/$1 [R=301,NC,QSA,L]
As in the previous option, although this rule-set redirects everything, the regex ^(.*) can be modified to match only certain URL-path pattern.
NOTES
The same directory structure and files in http://artygirl.co.uk/ must exist in http://brightmist.co.uk/ for any of the previous options to work.
If the actual .htaccess file in your question works as expected, you could use it in http://brightmist.co.uk/ root directory where the new WP is installed. Might require some modifications, though.
To move or copy a WP install, check this link Changing the site URL.
UPDATE:
From these sentences in your comment to this answer: "My domains both point at the same directory..." and "...now it creates a loop...", maybe the question is about domains pointing to the same content (Website), normally known as parked domains.
If that's the case, I am not sure redirecting in .htaccess the primary domain to the parked one is the correct approach just to change the domain name in the browser's address bar.
However, in theory something like this should do it using mod_rewrite directives:
Options +FollowSymlinks -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} artygirl\.co\.uk [NC]
RewriteRule ^(.*) http://brightmist.co.uk/$1 [R=301,NC,QSA,L]
Redirects permanently any request from http://artygirl.co.uk to http://brightmist.co.uk, appending the complete incoming path and query when present.
Since the .htaccess file is also shared, I think this rule-set should be placed at the top of the .htaccess file in the question, replacing the following lines:
RewriteEngine On
RewriteBase /
Are they redirecting to the same extension? Should be something like this:
RewriteEngine on
RewriteRule ^(.*)$ http://newdomain.com/$1 [R=301,L]
I see the problem. It redirects the root fine but if I go to:
http://artygirl.co.uk/photography/
I think you want it to go to
http://brightmist.co.uk/photography/
and not
http://brightmist.co.uk/
as it does currently.
Rewrite rules are notoriously hard to debug, but try this...
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !^brightmist\.co\.uk$ [NC]
RewriteRule ^(.*)$ http://brightmist.co.uk/$1 [R=301,L]
I know this is really similar to what Matthew Camp answered. The RewriteCond is very important to avoid endless loops because you are delivering both domains from the same folder. You only want to redirect the one that's incorrect.
Rewrite Base might also be required depending on how the servers are configured and what other rewrite rules exist at higher levels (invisible to you but which might still affect you). Try with and without the RewriteBase.
I hope this helps.
I'm having an issue with mod_rewrite where I want to match—and replace—a specific URL. The URL I want to rewrite is:
http://example.com/rss to http://example.com/rss.php
That means, if some one were to append anything after rss a 404 Not Found response be sent. Currently I'm using this mod_rewrite snippet:
Options -Indexes
RewriteEngine on
RewriteBase /
# pick up request for RSS feed
RewriteRule ^rss/?$ rss.php [L,NC]
# pass any other request through CMS
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+) index.php/$1
But this matches rss and rss with anything else added to the end. How can I re-write the above to acces only http://example.com/rss as the pattern for mod_rewrite to match against?
You are getting this error because /rss is being redirected twice in your rules by both RewriteRules. Have your rules like this:
Options +FollowSymlinks -MultiViews -Indexes
RewriteEngine On
RewriteBase /
# pick up request for RSS feed
RewriteRule ^rss/?$ /rss.php [L,NC]
# pass any other request through CMS
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (?!^rss\.php$)^(.*)$ /index.php/$1 [L,NC]
So with above rules it will redirect /rss OR rss/ URIs to /rss.php however /rss/foo will be redirected to /index.php since your 2nd rule is forwarding everything to /index.php
I was suprised to see that your rules just don't work, because in my first attempt I would have come to a very similar solution. But looking at the rewrite log revealed the real issue.
As discribed here the server prefers real files over directories. So internally rss/something becomes rss.php/something when applying the rewrite rules and things get weird.
So, one solution is to check if the Option MultiViews is enabled for the web directory either in .htaccess or in the vhost configuration. If so, remove it - which is what worked for me in this example.
If you need MultiViews, then I guess the only chance is to rename rss.php to rss-content.php and change the rule accordingly.
One additional note: you might want to add the following line after the # ... CMS block to prevent endless recursive calls.
RewriteRule ^index\.php/.* - [PT,L]
I hope this solves your rewrite problem.
I have a challenge using Apache.
In my .htaccess file I'd like to convert requests like this:
url/portfolio/filename.htm
to:
url?filename
Any takers? Thanks for your time
You have a couple of choices, depending on how you want the URL to appear to visitors (and search engines).
If you want the externally visible URL to remain url/portfolio/filename.htm, Alec's solution worked after I removed the two RewriteCond lines.
RewriteEngine On
RewriteRule ^(.*)/portfolio/(.*)\.htm$ $1?$2 [PT,L]
If other parameters could be in the query string and you want to preserve those, add QSA to the options in brackets at the end of the rule.
If you want people outside to see url?filename instead, change the rule to:
RewriteRule ^(.*)/portfolio/(.*)\.htm$ $1?$2 [L,R]
Same qualification about other query parameters applies.
If this still doesn't help, I suggest you turn on rewrite logging and look in that log for more clues. Post them here and someone will help. You might have to put this part in httpd.conf. My Apache didn't like it in .htaccess.
RewriteLog ...path...
RewriteLogLevel 3 # you'll regret anything higher
Something like this should work, although I have not tested it.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# Rewrite all other URLs to index.php/URL
RewriteRule ^(.*)/portfolio/(.*)\.htm$ $1?$2 [PT,L]
</IfModule>
<IfModule !mod_rewrite.c>
ErrorDocument 404 index.php
</IfModule>
This should work:
Options +FollowSymlinks
RewriteEngine on
RewriteRule ^portfolio/(.+)\.htm$ index.php?$1 [NC,L]
or if you want to visibly change it:
Options +FollowSymlinks
RewriteEngine on
RewriteRule ^portfolio/(.+)\.htm$ /?$1 [R,NC,L]
The second one would look like example.com/?filename to the user, whereas the first one would look like example.com/portfolio/filename.htm.
Hope that helps.