301 Redirect htaccess - php

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.

Related

Is it possible to get the original URL after a 302 .htaccess RedirectMatch?

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]

301 Redirect: index.php

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)

url rewrite with string in .htaccess

I am trying to use mod_rewrite to redirect users keeping the multiple query string and creating a redirect page
For Example,
If user opens
http://localhost/url/url/http://www.google.com/contacts/?user=abc&stackoverflow=great&google=facebook
then he is taken to
http://localhost/url/url.php?redirect=http://www.google.com/contacts/?user=abc&stackoverflow=great&google=facebook
There is secondary problem that URL should be encoded and then redirected! If URL is not encoded then the string (&stackoverflow=great)would be not a part of 'redirect' string of url.php
I tried many solutions then came for stackoverflow! I tried the following code in following file
http://localhost/url/.htaccess
RewriteRule ^url/([^/])$ url.php?redirect=$1 [QSA,L]
but the result is localhost/url/url.php?redirect=http only
Your setup won't work with the unencoded inner url, so an 'answer' can only have temporary character. But this might be a starting point:
RewriteEngine on
RewriteRule ^/url/url/(.*)$ /url/url.php?redirect=$1 [L,QSA]
I wonder however if that fragment /url/url is really intended (the two 'url's in there).
Note that the exact rule content also depends on where you want to define that rule. The syntax is different whether you use the central server configuration (referred) or .htaccess style files (as second choice and more complex).
Try this
RewriteEngine on
Redirect ^url/url/(.*)$ url/url.php?redirect=$1
The basic redirect systax,
redirect accessed-file URL-to-go-to

htaccess redirect url with var from no file extension to file extension

the problem is that for some reason googlebot gets a bunch of error 404s when crawling a certain page but everything works fine on my end, I think...
using htaccess, I have rewritten a page with vars for seo purposes, so:
RewriteRule ^feeds/([^/]*)\.xml/?$ /rss/feeds.php?cat=$1 [QSA,NC,L]
so I get www.url.com/feeds/category.xml
instead of
www.url.com/feeds.php?cat=category
I've also ajusted all urls on the page to follow this rule and like I said everything works fine on my end... googlebot for some reason only sees www.url.com/feeds/category and does see the .xml extention.
not sure if it would work but I was hoping to put a rule in htaccess to redirect www.url.com/feeds/category to www.url.com/feeds/category.xml in the even that this happened, but was unsure of how to do so.
Maybe try
RewriteRule ^feeds/([^/]+)/?$ /feeds/$1.xml [R,NC]
Which would redirect www.url.com/feeds/category to www.url.com/feeds/category.xml, using a HTTP 302 Redirect. Remove the R flag if you don't want it do use the HTTP 302 Redirect, but that would make www.url.com/feeds/category a valid URL as far as google is concerned. Just in case: mod_rewrite.

Codeigniter using routes & 301 redirects

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

Categories