301 Redirect: index.php - 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)

Related

htaccess redirect not working in same domain

I want to redirect https://xxx.xxxx.com/zzz to https://xxx.xxxx.com/zzz247. I have tried this
Redirect 301 / https://xxx.xxxx.com/zzz247
So now when I try https://xxx.xxxx.com/zzz its take me into https://xxx.xxxx.com/zzz247zzz247zzz247zzz247zzz247zzz247zzz247....
How can I fix this issue? help please
What is required
I need enable only one URL which is https://xxx.xxxx.com/zzz247 even user type https://xxx.xxxx.com/zzz
Instead of Redirect directive use RedirectMatch with regex support:
RedirectMatch 301 ^/zzz/?$ /zzz247
Redirect appends current URI into redirected URI and since you're matching / that will match anything and cause a redirect loop.
Make sure to clear your browser cache before testing.
Using RewriteRule you can do this:
RewriteRule ^zzz/?$ /zzz247? [L,NC,R=301]
? at the end of the target URI is for stripping any query string.

Redirect to a Rewritten URL

OK, so I'm rewriting some page URLs for a custom PHP cart.
I've got the following rules:
RewriteRule ^category/([0-9]+)/([a-z-]+)$ /store.php?cat=$1 [L]
RewriteRule ^product/([0-9]+)/([a-z-]+)$ /product.php?id=$1 [L]
These will allow me to use a url structure like example.com/product/23/product-slug.
That part is working alright. I'm wondering what options I have for the other direction; redirecting requests for the OLD url to the NEW url. So for example, when someone goes to /product.php?id=2 I want to redirect to /products/2/slug.
Any idea how to get this done?
I tried a simple redirect, but would not work:
Redirect 301 ^/store\.php\?cat=16$ http://www.example.com/category/16/category-slug
Redirect only takes a url prefix, not a regex (e.g. /store.php or /store)
You need to try RedirectMatch:
RedirectMatch 301 ^/store\.php\?cat=16$ http://www.example.com/category/16/category-slug
Also, is it supposed to start with a /? I'm not sure (your RewriteRule entries above start with no slash, for example)
I solved this a different way: with a modification to the store.php file.
I looked at output from print_r($_SERVER) after pinging both the normal and rewritten urls. I found that $_SERVER['SCRIPT_URL'] contains "/store.php" when the normal url is hit and it contains my rewritten path when the rewritten url is hit.
This means I can do a simple test and redirect appropriately:
if ($_SERVER['SCRIPT_URL'] == "/store.php") {
// run some code that will generate the url
$rewrittenURL = generateURL();
// then add:
header("HTTP/1.0 301 Moved Permanently"); // tell spiders this is permanent
header("Location: $rewrittenURL");
}

301 Redirect htaccess

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.

Redirect 301 / Rewrite conundrum

On my site, users can add various URL's that need to be redirected.
For example; from this: domain.com/oldpage/36/
To this: domain.com/newpage/47/
They are added to the .htaccess like this:
Redirect 301 /oldpage/36/ /new-page/47/
But when accessing the old page they get this:
domain.com/newpage/47/?pid=36&pagename=oldpage
I'm pretty sure these rewrite rules are causing this predicament:
RewriteRule ([^.]+)/([0-9]+)/$ index.php?pid=$2&pagename=$1
RewriteRule ([^.]+)/([0-9]+)/([^.]+) index.php?pid=$2&pagename=$1&vars=$3
However, mod_rewrite stuff is not my strongpoint, so I have no idea how to fix it.
Any ideas ?
Adding a ? makes the Rewrite not add the query string to the url.
so this should work:
Redirect 301 /oldpage/36/ /new-page/47/?
As a precaution you could also add it to the end of:
RewriteRule ([^.]+)/([0-9]+)/$ index.php?pid=$2&pagename=$1?
RewriteRule ([^.]+)/([0-9]+)/([^.]+) index.php?pid=$2&pagename=$1&vars=$3?
But only if they are needed
Since you are already using mod_rewrite anyway, I suppose you should make your redirects using rewrites too
RewriteRule /oldpage/36/ /new-page/47/ [R=301]
This will "rewrite" the URL from old to new, and will redirect the browser to new url with status code 301. [R] directive means redirect, which also stops other rules from processing, hence the other rules will be handled only when the new request is sent from broswer with new url.

.htaccess 301 redirect not working

I have a basic CMS in PHP/MySQL where content managers can create pages to the system for public viewing. Each page is then available at an url such as http://www.example.com/pages.php?pid=123
Now, I want to redirect requests to http://www.example.com/pages.php?pid=123 to http://www.example.com/pages.php?pid=456.
I've already removed the pid=123 page from the db but because of the cms code the site still returns a 202 when some one tries to access the page. I thought I could use a 301 redirect in .htaccess to make the redirect work, i.e.:
redirect 301 pages.php?pid=123 http://www.example.com/pages.php?pid=456
but this doesn't work, Apache still return 202 when trying to fetch the pid=123 page. Also, I've tried using mod_rewrite, but it doesn't work:
RewriteRule ^pages.php?pid=123$ pages.php?pid=456 [R=301,L]
Any ideas what could be wrong and how I can fix the 301 redirect?
Both the Redirect and RewriteRule directive work just on the URL path. In mod_alias (Redirect directive) you can not test the query and in mod_rewrite (RewriteRule directive) you need an additional RewriteCond directive:
RewriteCond %{QUERY_STRING} (^|&)pid=123(&|$)
RewriteRule ^pages\.php$ /pages.php?pid=456 [R=301,L]
But it would certainly be better if your CMS can handle such redirects since it’s your CMS that knows best what URLs are valid and what are not.
You can perform the redirect in PHP (which probably knows more about what to redirect where) using header().
Please note that ? is a special character used by regular expressions, so your regex matches pages.phppid=123 and pages.phppid=123.
Even then, I don't think the query string (including the ?pid=123 part) is used in the URL handled by RewriteRule, so you would need to use something like:
RewriteCond %{QUERY_STRING} ^pid=123$
RewriteRule ^pages.php$ pages.php?pid=456 [R=301,L]
This shouldn't work as is, but it should give you some ideas.

Categories