I am in the process of deploying a new site which needs 301 redirects setup for existing links indexed by Google.
Can anyone suggest how using the routes feature & regular expression I force any links to use the new 301 redirect.
E.g. a route
$route['hotel/pages/([a-z0-9_-]+)//\.htm$/'] = 'hotel/page/redirect/$1/$2';
So i'd like any pages that following the following structure:
site.com/hotel/pages/somesection/page.html
to use another controller where i'll setup the 301 redirect (I can do this bit)
Can anyone give me some suggestions?
This should do (built a quick local test and it worked):
$route['hotel/pages/(:any)/(\w+.html$)'] = 'hotel/page/redirect/$1/$2';
:any (a CI's thing) is much like /\w+/i so it matches anything in the 3rd segment, and remaps to the $1; then you have a slash and then any character, repeated more times, and ending in '.html', which remaps to $2
On apache, if the mapping is simple enough to describe with a regular expression, you could just use mod_rewrite and skip the redirect controller.
For Example:
RewriteEngine On
RewriteRule ^hotel/pages/(.*)/(.*\.html?)$ http://site.com/hotel/$1/$2 [R=301,L]
Would give you a 301 from
site.com/hotel/pages/somesection/page.html
to
site.com/hotel/somesection/page.html
Related
Hello guys. I started coding my own "URL shortener". The basic idea is you use example.com/12345 to redirect to another URL. This "matching" is done by using .htaccess to redirect stuff towards a script that does (irrelevant for us now) stuff.
My .htaccess currently looks like this:
RedirectMatch 302 ^/\w{5}$ /redir.php
The redirect matches any string of exactly 5 and sends it toward my PHP script where the actual redirection to the expanded URL take place. The only problem is that I was unable to find a proper way of getting the original URL, the matched one into a variable.
As a sidenote the whole thing happens on a VPS set up by me with minimal knowledge, so if this problem can originate from a missing config ($_SERVER['HTTP_REFERER'] doesn't work), then expect my configs to not be 100% correct and by standards.
EDIT: changed from RedirectMatch to RewriteRule, still doesn't work.
RewriteRule ^\w{5}$ /redir.php [R,L]
you can use the following rule:
RewriteRule ^(\w{5})$ /redir.php?redir=$1 [R,L]
this will send the 5 letter string as querystring param redir. Which you can access in redir.php as:
$_GET['redir']
Edit: Or as #LawrenceCherone have suggested you can use $_SERVER['REQUEST_URI'] in redir.php. But for that you have to use NC flag in .htaccess instead, Like:
RewriteRule ^(\w{5})$ /redir.php [NC,L]
I have a very peculiar requirement, hopefully I can explain it without being too confusing. I created a page template where I list some state's city, let's say the URL is like this: http://www.example.com/states/?q=ohio
and i would like to do it like http://www.example.com/states/ohio/
i also used add_rewrite_rule but it's does not given me output that i want.
so how could i do fix ?
It's actually not PHP, it's apache using mod_rewrite. What happens is the person requests the link, http://www.example.com/states/ohio/ and then apache chops it up using a rewrite rule making it look like this, http://www.example.com/states/?q=ohio, to the server. You can find more here: Rewrite Guide
Web server side
Since you're running Apache, you can create a RewriteRule to rewrite the URI to something that the application understands but have vanity/pretty URIs to your endusers.
For example;
RewriteEngine On
#Rewrites a request like http://www.example.com/states/ohio/ to http://www.example.com/states/?q=ohio at application level
#Having the /? at the end makes the ending slash optional
RewriteRule ^states/([^\/]+)/?$ /states/?q=$1 [L]
Tested with http://htaccess.mwl.be/
Wordpress side
You can use add_rewrite_rule to rewrite the URLs as you desire
add_rewrite_rule('^states/([^\/]+)/?$', 'states/?q=ohio$matches[1]', 'top');
Otherwise, you'd have to manually modify each href element on an anchor tag (<a />) to point to /states/ohio/ instead of /states/?q=ohio.
I am familiar with redirecting URLs that don't have file types at the end, but not with ones that have .php at the end.
The website I'm working on has hundreds of indexed pages on Google that have this at the beginning of the url: http://example.com/index.php? with more information trailing afterwards.
An example is: http://example.com/index.php?main_page=index&cPath=1_18
The new index of the website is http://example.com/index or http://example.com
If I put http://example.com/index.php as the URL to be redirected, will it also redirect all index.php? Or do I need to put http://example.com/index.php* as the URL to be redirected or http://example.com/index.php?*?
Thank you!
If the main script to be executed is index.php, then there is no point in redirecting a url to itself...
Aabout those GET parameters in the url, if index.php oesn't use them, then.... they will be useless, but... thats it XD
The point is: does everything work without redirecting? If so, why redirecting at all?
If there are 301 redirects, ... that will mean more request to the server, and for those clients with low bandwith it does make a difference.
BTW, take a look at: htaccessredirect
Try:
RedirectMatch 301 ^/index\.php$ /
(or)
RedirectMatch 301 ^/index\.php$ /index
The query string at the end will automatically get appended.
If you're using wordpress now, you're not going to be able to use the mod_alias directive like above. You'll need to use mod_rewrite and place these rules above the wordpress rules:
RewriteCond %{THE_REQUEST} \ /+index\.php
RewriteRule ^ / [L,R=301]
(or replace the / at the end with /index)
It happens that I need to fix a Codeigniter issue urgently while being in no way familiar with the tool.
Simple question: how do I allow links back from Facebook like http://www.xxx.de/?fb_action_ids=4811819099741&fb_action_types=og.likes&fb_source=timeline_og&action_object_map=%7B%224811819099741%22%3A447766801925104%7D&action_type_map=%7B%224811819099741%22%3A%22og.likes%22%7D&action_ref_map=%5B%5D
without creating either the infamous The URI you submitted has disallowed characters - which I can fix by setting $config['permitted_uri_chars'] - and neither a 404 because of the internal redirects of CI.
I'd love to learn of a quick fix for that issue.
If you can use .htaccess you can redirect to wherever. This avoids any CI intricacies.
RewriteCond %{QUERY_STRING} .
RewriteCond %{REQUEST_URI} !^/index.php
RewriteRule ^ /index.php/? [L]
https://stackoverflow.com/a/4227616/183254
update: possible working rewrite for nginx
if ($args ~ fb_action_ids=(\d+)){
rewrite ^ http://example.com/ permanent;
}
Set these vars in application/config/config.php:
$config['allow_get_array'] = TRUE;
$config['enable_query_strings'] = TRUE; // This is normally FALSE
Adjust $config['permitted_uri_chars'] if necessary. Mine are a-z 0-9~%.:_\-#! and work for almost everything.
I'm not sure what you need to do with the fb data, so all I can say is that with those options set, you should, in theory, be able to access the get vars supplied by facebook.
codeigniter expects the first part of the URI to be the controller it loads, then the second will be the method, and every segment after that are the parameters for the method that gets loaded. A fast solution is set up a route with a regular expression like you would in MVC3 and point it to a controller and method. You can then parse the uri from there.
Match ?fb_action_ids and change it to /controller/method/?fb_action_ids and go from there. Perhaps with a little more information I could help you even further but if this works for you this should be the quickest way to get up back up and running without having to reconfigure a bunch of things.
http://ellislab.com/codeigniter/user_guide/general/routing.html
im having a problem with my htaccess
I have the following
Redirect 301 /inspection-test.html http://www.newsit.co.uk/inspection-test.aspx
My current site runs on php and the new one runs on .net
The redirect works but its showing as
http://www.newsit.co.ukinspection-test.html
You can try to use RedirectMatch Apache HTTPD Docs instead:
RedirectMatch 301 ^/inspection-test\.html$ ttp://www.newsit.co.uk/inspection-test.aspx
Redirect Apache HTTPD Docs will add the original URL-part to the end of the redirect URL which produces the unexpected output in your case on your server.
With RedirectMatch you can better control the behavior.
Strange.
The url-part appended at the end of the path should only happen i your Redirect first argument is a directory, not for a file.
Now while you're testing the rewrites you shouldn't use 301 codes, as your browser will store the 1st result (so your first test) and will never ask the server for the fixed mapping (if you fix it). So it can be as well that you need to switch off your browser before redoing the redirect test. So maybe your rule is OK but your browser is not testing it.
While you're testing you should use 302 redirect codes.
Theres a lot of pages and all the url's differ
You should study RewriteMap. If you can list quite easily the old url and map each one to a new url url then a txt: map will dop the job, first with 302 codes, and when you're ok with it use a 301 code with a hash map file instead of a text one.
RewriteMap oldtonew txt:/path/to/my/hashmap/oldtonew.txt [NC]
# detect new url from map file
RewriteCond ${oldtonew:%{REQUEST_URI}|NotFound} !NotFound
RedirectRule .? ${oldtonew:%{REQUEST_URI}} [NC,R=301]
#no mapping
RewriteCond ${oldtonew:%{REQUEST_URI}|NotFound} NotFound
RedirectRule .? http://www.example.com/notfound.html [NC,R=301]
In oldtonew.txt write things like that
/inspection-test.html http://www.newsit.co.uk/inspection-test.aspx
/inspection-foo.html http://www.newsit.co.uk/inspection-bar.aspx
Else if you can express the old-to-new mapping with some keywords in the old url, the DirectoryMatch could maybe do the trick, or some mod-rewrite with the good extractions. But the faster solution is the rewriteMap with an hash file (dbm).
Finally If you can express your old-to-new url mapping only with a with a program (a database?) you'll have to use the prg: keyword (quite hard) in rewriteMap.