301 Redirect - variable in the old url - .htaccess

I have several urls on a Joomla site which have been indexed and I need to 301 redirect them into some new pages. The old URL is formed like this:
http://www.mydomain.com/en/wfmenuconfig/family/family-disease/177-category-english?start=20
I want it to go to:
http://www.mydomain.com/en/family-members/family-disease
I tried using:
RewriteCond %{QUERY_STRING} ^start=(.*)$
RewriteRule ^/en/wfmenuconfig/family/family-disease/177-category-english$ http://www.www.mydoamin.com/en/family-members/family-disease%1 [R=301,L]
I've tried several answers on here but nothing seems to be working.
htaccess 301 redirect dynamic url
and
301 Redirecting URLs based on GET variables in .htaccess
Any ideas what I should try next? (I've tried a normal redirect 301)

You've almost got it. You need to remove the leading slash from your rule's pattern because it's removed from the URI when applying rules from an htaccess file:
RewriteCond %{QUERY_STRING} ^start=(.*)$
RewriteRule ^en/wfmenuconfig/family/family-disease/177-category-english$ /en/family-members/family-disease%1? [R=301,L]
You also don't need the http://www.www.mydoamin.com bit (2 sets of www). At the end of your target, you have family-disease%1, which means if start=20 then the end of your URL will look like: family-disease20. Is that right?

The new URL doesn't have the query string in it, so it is just stripping of the last URL path part. If you want it hardcoded
RewriteCond %{QUERY_STRING} ^start=
RewriteRule ^en/wfmenuconfig/family/family-disease/177-category-english$ /en/family-members/family-disease? [R,L]
or a little bit more flexible
RewriteCond %{QUERY_STRING} ^start=
RewriteRule ^en/wfmenuconfig/family/family-disease/.+$ /en/family-members/family-disease? [R,L]
or if you just want to keep two levels after en/wfmenuconfig
RewriteCond %{QUERY_STRING} ^start=
RewriteRule ^en/wfmenuconfig/(.+?/.+?)/ /en/$1? [R,L]
Never test with 301 enabled, see this answer Tips for debugging .htaccess rewrite rules for details.

If you just want to redirect http://www.mydomain.com/en/wfmenuconfig/family/family-disease/177-category-english?start=$var into http://www.mydomain.com/en/family-members/family-disease, then you must try these directives:
# once per .htaccess file
Options +FollowSymlinks
RewriteEngine on
RewriteCond %{QUERY_STRING} start=([0-9]*)
RewriteRule ^en/wfmenuconfig/family/family-disease/177-category-english /en/family-members/family-disease [R=301,L]
But if that's not what you want, but to redirect http://www.mydomain.com/en/wfmenuconfig/family/family-disease/177-category-english?start=$var into http://www.mydomain.com/en/family-members/family-disease$var then you could check this one:
# once per .htaccess file
Options +FollowSymlinks
RewriteEngine on
RewriteCond %{QUERY_STRING} start=([0-9]*)
RewriteRule ^en/wfmenuconfig/family/family-disease/177-category-english /en/family-members/family-disease%1 [R=301,L]
Now, give this one a little more try if it will work. If it's not, then find any suspicious why this code is not working:
Options +FollowSymlinks
RewriteEngine on
RewriteBase /en/
RewriteCond %{QUERY_STRING} start=([0-9]*)
RewriteRule ^wfmenuconfig/family/family-disease/177-category-english /family-members/family-disease [R]
And go to http://www.mydomain.com/en/wfmenuconfig/family/family-disease/177-category-english?start=$AnyNumber if it's redirecting into http://www.mydomain.com/en/family-members/family-disease just make sure that your web server have mod_rewrite.

I just wanted to throw this out there, I was also having trouble getting the RewriteRule to work. I have a client that upgraded to a WordPress powered site from .asp pages. What I had to do to get this to work is insert the RewriteCond and RewriteRule in the htaccess file BEFORE the "# BEGIN WordPress" section. Now it works just as it should.
This is posted way late, but hopefully it helps someone else out there running into the same issue.
Doesn't Work:
# 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 %{QUERY_STRING} ^var=somestring$ [NC]
RewriteRule ^oldpage\.asp$ http://www.domain.com/newpage? [R=301,L]
Does Work:
RewriteCond %{QUERY_STRING} ^var=somestring$ [NC]
RewriteRule ^oldpage\.asp$ http://www.domain.com/newpage? [R=301,L]
# 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
Order of operations must be important =)

Related

Cancel rewrite and redirect removing last directory with htaccess

I have a page with this settings:
Options All -Indexes
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /demo/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} -d
RewriteCond %{REQUEST_URI} !^(.*)show(/|.*)$
RewriteRule . /demo/index.php [L]
</IfModule>
For example, when you request the page http://example.com/demo/any_folder/ it loads http://example.com/demo/index.php without changing the URL, to retrieve data from a database for example. Well, this works.
But I want to add and exception.
I need a condition that if you visit http://example.com/demo/any_folder/show it loads the same URL removing the word "show" but avoiding the redirect that is actually defined. So, it will load http://example.com/demo/any_folder/ and not http://example.com/demo/index.php. Does anyone know how to do this?
There has to be something different about the URL to not be redirected to index.php. The following .htaccess code adds a query parameter ?show to tell the two URLs apart.
Since, the redirection is internal; the user never gets to see the parameter.
RewriteEngine On
RewriteBase /demo/
RewriteRule ^index\.php$ - [L]
RewriteRule ^(.*)/show/?$ $1?show [NC,QSA,L]
RewriteCond %{REQUEST_FILENAME} -d
RewriteCond %{QUERY_STRING} !^show [NC]
RewriteRule . /demo/index.php [L]

Please take a look at this .htaccess file

I am trying to implement 301's for sub-pages on my site which is hosted on an Apache server (Fasthosts), using Rewrite scripts in the .htaccess file. I have tried to follow many pieces of documentation (infact, I've never had any problems implementing 301's using .htaccess before!) but on this particular website, nothing seems to work.
There is currently a 301 re-direct in there from non-www to www which is working fine. There are also some other snippets using regex which I imagine are for the CMS.
Below is the current state of the .htaccess file. An example of a 301 I'm trying to re-direct is in there (Lines 5 & 6)
Old page: http://www.junkwize.com/home-Garden%20Clearance
to New page: http://www.junkwize.com/services/garden-clearance-london
.htaccess file:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^junkwize.com
RewriteRule (.*) http://www.junkwize.com/$1 [R=301]
RewriteCond %{HTTP_HOST} www.junkwize.com/home-Garden%20Clearance
RewriteRule (.*) http://www.junkwize.com/services/garden-clearance-london [R=301]
#Options +FollowSymlinks
RewriteRule ^.htaccess$ — [F]
RewriteRule ^([/admin]+)$ admin/login.php [L]
RewriteRule ^([/admin]+)([/blocks]+)$ admin/login.php [L]
RewriteRule ^([/blocks]+)$ index.php [L]
# RewriteRule ^([^/\.]+)-([^/\.]+)-([^/\.]+)$ index.php?main=$1&id=$2&menu=$3 [L]
RewriteRule ^([/deals]+)-([^/\.]+)$ index.php?main=$1&id=$2 [L]
# RewriteRule ^([^/\.]+)-([^/\.]+)$ index.php?main=$1&leftmain=$2 [L]
RewriteRule ^([^/\.]+)/([^/\.]+)$ index.php?main=$1&leftmain=$2 [L]
RewriteRule ^([^/\.]+)$ index.php?main=$1 [L]
Any help would be much appreciated.
Many thanks.
This code is faulty and is not going to work:
RewriteCond %{HTTP_HOST} www.junkwize.com/home-Garden%20Clearance
RewriteRule (.*) http://www.junkwize.com/services/garden-clearance-london [R=301]
Reason is that RewriteCond %{HTTP_HOST} can only match host name. Replace that code with this:
RewriteRule ^home-Garden\ Clearance/?$ /services/garden-clearance-london [R=301,L,NC]

Rewrite rule not working as expected?

I have a URL with a parameter which I wish to make into sef URL:
want:
http://map.tautktiv.com/street.php?address=abc
to become:
http://map.tautktiv.com/street/address/abc
or
http://map.tautktiv.com/address/abc
have tried several online tools to generate a .htaccess rule, but none of them have any effect on the URL, .htaccess file is active (tried to put some gibberish in it and got error 500)
these are the rules I tried:
1.
Options +FollowSymLinks
RewriteEngine on
RewriteRule ^address-([^-]*)$ /street.php?address=$1 [L]
RewriteRule street/address/(.*) street.php?address=$1
2.
Options +FollowSymLinks
RewriteEngine on
RewriteRule /address/(.*)\.php street.php?address=$1
3.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# add whatever other special conditions you need here
RewriteRule ^/([0-9]+)-(.*)$ /street.php?address=$1 [L]
RewriteRule /(.*)/(.*)/$ street.php?address=$1
the site is a sub-domain which files reside in a sub directory in a shared hosting GoDaddy server, have also tried to apply these rules to the .htaccess in the directory above it, same result.
tried also this per below suggestions
Options +FollowSymLinks
RewriteEngine on
RewriteBase /
RewriteRule ^street/address/(.*)$ street.php?address=$1 [r=301,L]
RewriteRule ^address/(.*)$ street.php?address=$1 [r=301,L]
RewriteRule ^street/address/(.*)$ street.php?address=$1 [r=301,L]
same result, nothing happens.
tried to go directly to page from main domain but same result:
http://tautktiv.com/map/streets/street.php?address=abc
First rule will redirect your ugly URL to the pretty URL.
Second rule will internally redirect it back so the user will not see the ugly URL.
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /
# Internally forward /street/address/abc to /street.php?address=abc
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^street/address/(.*)/?$ /street.php?address=$1 [NC,L]
# Internally forward /address/abc to /street.php?address=abc
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^address/(.*)/?$ /street.php?address=$1 [NC,L]
If you confirm the rule to be working as expected then you can change it from 302 to 301 as you do not want to use 301 until you know the rule is working as expected.
The .htaccess should go inside the folder where street.php is located.
HTTP is US ASCII so your language would fail, it will redirect it to something like this:
/street/address/%25D7%2590%2520%25D7%2598%25D7%2591%25D7%25A8%25D7%2599%2520%25D7%2599%25D7%25A8%25D7%2595%25D7%25A9%25D7%259C%25D7%2599%25D7%259D%2520%25D7%2599%25D7%25A9%25D7%25A8%25D7%2590%25D7%259C
Your best bet here would be to change the links to use /street/address/word instead of the php file directly.
This way you would not need the first rule and you can use only the internal redirect which would work just fine with this update.
Try this one:
RewriteEngine on
RewriteRule ^street/address/(.*)$ street.php?address=$1 [r=301,L]
RewriteRule ^address/(.*)$ street.php?address=$1 [r=301,L]
In your examples you'd missed ^ and $ in the second row of RewriteRule.
And use [r=301,L] instead of [L] to tell the browser, that thzis is premanent redirecting.

htaccess rewrite rule, old URL to new

A bit of help fellow SO people.
What I have at the moment (based on some code I used for a different type of URL).
I want the first URL to redirect to the second, with no query string included afterwards
This is what I have to so far.
RewriteRule ^(page.php?id=missionstatement+)/?$ http://example.com/why/mission-statement [R=301,L]
RewriteRule ^(page.php?id=ofsted+)/?$ http://example.com/how/ofsted-report [R=301,L]
RewriteRule ^(page.php?id=governingbody+)/?$ http://example.com/governors [R=301,L]
Here is the rule (will redirect 1 URL):
RewriteCond %{QUERY_STRING} ^id=whatever
RewriteRule ^page\.php$ http://%{HTTP_HOST}/how/somehow? [R=301,L]
This rule intended to be placed in .htaccess in website root folder. If placed elsewhere some small tweaking may be required.
I have used %{HTTP_HOST} -- this will redirect to the same domain as requested URL. If domain name has to be different, replace it by exact domain name.
The ? at the end of new URL will get rid of existing query string.
Ahoy!
Give this a whirl:
#check mod_rewrite is enabled
<IfModule mod_rewrite.c>
#enable mod rewrite
RewriteEngine On
#set working directory
RewriteBase /
#force trailing slash
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ $1/ [R=301,L]
#bootstrap index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^page.php\?id=(.*)$ http://www.willans.com/page.php/$1 [R=310,L]
#end mod rewrite check
</IfModule>
It's been a while since i've done any web dev, but that should be a push in the right direction at least ;)

301 redirect from query string to simple URL

I've had a good look through the first ten pages of search results for "301 redirects" and can't find the answer so here goes...
I've moved from a crappy old CMS that didn't give my pages nice URLs to one that does and I want to set up a 301 redirect for my key pages.
Current URL: http://www.domain.com/?pid=22
New URL: http://www.domain.com/contact/
I'm using Wordpress and my current htaccess file looks like this:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
Any help would be awesome!
Give this a try. All you need to do is check to see if you are on page X and then redirect to page Y. Consider RewriteCond statements to be 'if' statements. If you need more redirects just duplicate the last two lines and edit the paths.
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !^www\.domain\.com\/?pid=22$ [NC]
RewriteRule ^(.*)$ http://www.domain.com/contact [L,R=301]
You have to check the query string for the value in the "pid" variable and then redirect if the value in that variable matches a page you want to redirect. You can do this with the "RewriteCond" and "RewriteRule" directives like this:
RewriteEngine On
RewriteBase /
# Redirect pid=22 to http://www.domain.com/contact
RewriteCond %{QUERY_STRING} ^pid=22$
RewriteRule ^(.*)$ http://www.domain.com/contact [R=301,L]
You can repeat the "RewriteCond" and "RewriteRule" directives to create additional redirects.

Resources