Custom 301 Redirects using Regex - php

I've done some research on this and cannot find an good solution. I come from an ASP.net background and am moving our site to WordPress.
My old portfolio link format is as follows
/custom-website-design/airline-aviation/5
The same link on the new website will be
/custom-website-design/airline-aviation
Note the last "/5" is not there. On my old site I used this to look up the category on the back end.
Now my question is, in wordpress, using either a plugin or a custom .htaccess file, can I have the site look for
/custom-website-design/(ANYTHING)/(NUMERIC)
and then have it redirect to
/custom-website-design/(ANYTHING)
Thanks again!

It can be done using WordPress rewrite module but i'm not an expert.
Anyway, you can also do it using .htaccess and mod_rewrite.
You can put this code in your htaccess (which has to be in root folder)
RewriteEngine On
RewriteRule ^custom-website-design/([^/]+)/[0-9]+$ /custom-website-design/$1 [R=301,L]
Note: since you're using WordPress, you have to put this rule before WordPress' main rule.
Should look like this finally
RewriteEngine On
RewriteRule ^(custom-website-design/[^/]+)/[0-9]*$ /$1 [R=301,L]
# WordPress
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ /index.php [L]

Related

Redirect URL Rewrite/Map from .htaccess

The problem is,
I want to port my current website which is built upon CodeIgniter to WordPress. I do not want to hurt my google ranking and for that, I really need to map certain URLs to an existing file and for new URLs, I will let WordPress handle it in the default way. The main problem that arises is that I do not wish to change the existing domain. I want to redirect/map the files on the same domain from my existing CodeIgniter project.
Okay, let me make it a bit more clear about the state of my problem. I copied my existing CodeIgniter project into the WordPress root folder. Now, I will let the old URL being served from my CodeIgniter project and for all the new ones I will let WordPress handle it. I will also port the existing database to the new server and create a separate one for the WordPress installation.
So, how can I map my old URLs to the CodeIgniter?
My old URLs looks something like this,
http://www.example.com/site/blog/123/abc-xyz-wxy
I want to handle these URL from CodeIgniter file and the new URLs that will be created by WordPress will be handled by WordPress which would look like this,
http://www.example.com/abc-xyz-wxy
So, far this is how my .htaccess file looks like,
BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /cms/wordpress/
RewriteRule ^site/blog/([0-9]+)/([a-zA-Z0-9-]+)/?$ codeignitor/index.php/$2
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /cms/wordpress/index.php [L]
</IfModule>
END WordPress
And this thing works absolutely fine, but what I want is more of a URL masking. I want to eliminate the CodeIgnitor folder name from the URL.
For selecting the "abc-xyz-wxy" string and redirecting, try using:
RewriteCond %{REQUEST_FILE} !-f
RewriteCond %{REQUEST_FILE} !-d
RewriteRule ^site\/blog\/123\/([a-z-]+) index.php\/$1 [NC,L] ^$1
Disclaimer: Never used CodeIgniter, so everyone feel free to correct.
In case anyone else happens upon this, I have a similar sounding use case where the application code resided inside /public_html/app/ and I wanted it redirect from /public_html/ but not show the app/ portion of it in the url. There was also an additional requirement to not route specific urls in this manner. The resulting file is below. Hopefully it helps someone on a similar journey.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
## SSL
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
## DO NOT ROUTE THESE DIRECTORIES
RewriteRule ^(path-1|path-2) - [L]
## ROUTE OTHER REQUESTS TO /APP
RewriteCond %{ENV:REDIRECT_STATUS} . [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]
RewriteCond %{REQUEST_URI} /(.+)
RewriteRule !\.[a-z0-4]{2,4}$ /app/index.html [NC,L]
RewriteRule (.*) /app/$1 [L]
</IfModule>

Htaccess proxy redirect wordpress error

I want to redirect domainA.com/folder/ which originates within an older wordpress installtion to domainB.com/folder1/folder2/ which is the link to the page within my new wordpress installation wihtout the url changing.
I have managed to achieve this with the .htaccess file inside domainA.com/folder/ which reads:
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/folder
RewriteRule ^(.*)$ http://www.domainB.com/folder1/folder2/ [P]
The Rewriterule [P] (proxy) flag does what I want and redirects the page without changing the url but some of the content within my Enfold WP template such as the advanced layer slider and icons fail to load.
I assume this error is due to some sort of conflict with WP's native htaccess code and I also suspect it has to do with the lines:
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
I have experimented with adding the first, third and fourth lines to my code but it doesnt work. Does anybody know how I could resolve this issue?
If it makes any difference the wordpress site I want to direct to (domainB.com/folder1/folder2/) is one whithin a Wordpress Network.
For the moment I have changed the [P] flag to [L] flag which redirects and works but changes the url.
Any help or advice would be much appreciated. Thank you!
Try to change the paths of images (the URLs) in advanced layer slider and icons URLs from
relative to absolute ones (as described in RFC 2396), then you can use the [P] flag in .htaccess again.
Often plugins uses relative paths when outputting content, but this can cause such type of troubles.
This following code takes care of permalinks in WorPress and it is recommended to keep the beginning and ending WordPress lines so that WordPress is able to locate it and modify it as needed. You may already have all this and are just not displaying it. I do not know.
# 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
In the other section you probably need to remove the trailing / after folder2 like so:
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/folder
RewriteRule ^(.*)$ http://www.domainB.com/folder1/folder2 [P]

htaccess file not working for wordpress pages

I'm trying to add a new rule in the htaccess file that will allow input of other instead of the actual name of the page which is notify. But I can't seem to get it to work.
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteRule ^other$ notify [QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
Also tried something like:
RewriteRule ^other$ index.php/notify [QSA]
and this:
RewriteRule ^other$ /index.php/notify [QSA]
Also tried adding the rules from wordpress itself (inside the admin_init section of a plugin) and then refresh the rules by going to the permalinks settings, choosing post name and then save but no luck either. Upon checking htaccess file it produced a rule similar to the above one:
add_rewrite_rule('other', 'index.php/notify', 'bottom');
I've verified that I can override the rules via the htaccess file in the root directory because when I tried to use an actual file in the server hello.php
RewriteRule ^other$ hello.php [QSA]
It works. I just don't know why when I use a wordpress page it doesn't work. It also seems to work when I use the absolute path:
RewriteRule ^other$ http://somesite.com/notify [QSA]
But this is no good because when I add some arguments to the URL (which is what I will ultimately do) I end up with bad looking URL when I do the above: http://somesite.com/?some_argument=some_value
The current permalink setting in wordpress is set to post name. Any ideas what I have missed?
Try this rule:
RewriteRule ^other/?$ index.php?p=PAGE_ID [QSA]
Put the necessary PAGE_ID to redirect to
Hello may be you can use .htaccess plugin..! to control it from admin panel. I hope this Help you
http://wordpress.org/plugins/wp-htaccess-control/

I'm having problems with RewriteRule

I have a site I'm trying to develop and I am having some (probably noob) problems getting my reWrite rules to work correctly.
Basically, I'm using the codeIgniter framework to develop the site and as part of this I have created a RewriteRule to remove the index.php part of the URL with the exception of some folders for images etc.
RewriteEngine on
RewriteCond $1 !^(index\.php|images|css|js|robots\.txt|shop)
RewriteRule ^(.*)$ /TESTSite/index.php/$1 [L]
The site originally has a current page of page/facilities/our-shop which was a very basic page with a few links to paypal to make a very rough shop page as we needed one quickly. However now I have implemented a different (read OpenCart) shop section to make this a bit more professional and allow more features etc. I have placed the openCart in a subdirectory called shop. The shop continues to work correctly as I've excluded the /shop from my original reWrite rule.
However - My problem...
I want the old page page/facilities/our-shop to redirect to the /shop and therefore work as if I navigated directly there..
I have tried the following:
RewriteEngine on
RewriteCond $1 !^(index\.php|images|css|js|robots\.txt|shop)
RewriteRule ^page/facilities\/our-shop$ /TESTSite/shop/$1 [R,NC,L]
RewriteRule ^(.*)$ /TESTSite/index.php/$1 [L]
And
RewriteEngine on
RewriteCond $1 !^(index\.php|images|css|js|robots\.txt|shop)
RewriteRule ^page/facilities\/our-shop$ /TESTSite/shop/$1 [N]
RewriteRule ^(.*)$ /TESTSite/index.php/$1 [L]
And with a few other variations, but every time I can't get it to work. Can anyone advise please?
Thanks
I think you can try the following rules:
RewriteEngine on
RewriteRule TESTSite/page/facilities/our-shop$ /TESTSite/shop/$1 [R=301]
RewriteCond %{REQUEST_URI} !^(index\.php|images|css|js|robots\.txt|shop)
RewriteRule ^TESTSite/page/(.*)$ /TESTSite/index.php/$1 [L]
Two things:
When you use RewriteCond, it only applies to the following rule. So I re-ordered the two lines.
You do not need \ before a / in a rule.
Check this tool to debug your rules.

htaccess html > php redirect + Wordpress rewrite permalinks structure

I have started a site up using html only, switched it over to php to make my life easier and have used the following code in the .htaccess to ensure all users (who may have bookmarked) are still seeing the right pages:
Options +FollowSymlinks
RewriteEngine on
RewriteRule ^(.+)\.html$ http://domain.com/$1.php [R,NC]
Now I am working on the news section a wordpress blog (inside a /news directory) that would be lovely to use permalinks with, when I slap the rewrite code below into my .htaccess all my files outside of the wordpress directory default to wordpresses 404, they still have the correct address in the bar, but no information.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /news/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /news/index.php [L]
</IfModule>
Can anyone help on the correct structure for the .htaccess - I tend to use snippets for specific tasks rather than write htaccess stuff.
Thanks
Your rules have to be come before the Wordpress rules and you need to add an [L] to your RewriteRule:
RewriteRule ^(.+)\.html$ http://domain.com/$1.php [R,NC,L]
Which will tell .htaccess to stop processing rules.
I would recommend a permanent (301) redirect so Google and the other search engines will update their listings:
RewriteRule ^(.+)\.html$ http://domain.com/$1.php [R=301,NC,L]

Categories